she's apples
Today we looked at developing algorithms.
This was followed by a quick look at JavaScript in JavaScript Basics, Part 1.
Extending the lesson a little we came up with the following super-apple-eating algorithm...
<script type="text/javascript">
var apples = 5;
alert('there are currently ' apples ' apples');
var eat = prompt('How many apples would you like to eat?', '1');
if (parseInt(eat))
{
if (parseInt(eat)>apples)
{
alert("don't be greedy!");
} else {
apples -= parseInt(eat);
if (apples > 0)
{
if (apples == 1)
{
alert('There is only one apple left!');
} else {
alert('Now there are only ' apples ' apples!');
}
} else {
alert('no more apples for you!');
}
}
} else {
alert('there are still ' apples ' apples');
}
</script>
Image: 'Granny Smith'
www.flickr.com/photos/90581837@N00/78379799
No comments:
Post a Comment