The aim of this puzzle: Take 10 seeds away from the Ant and give them to the Grasshopper.
Walkthrough of the solution: The first 3 lines of code create the 3 variables: ant
who has 40 seeds, grasshopper
who has 0 seeds, and gift
which has 10 seeds. The ant
is paying for the gift, so the value of ant
should go down by 10. The -=
operator is used to decrease a variable’s value. The grasshopper
is receiving the gift
, so it gets the gift
added to its value. Using grasshopper += gift
will add the gift
value (10) to grasshopper (0 --> 10).
Sample code solution:
(Tap below to reveal)
let ant = 40;
let grasshopper = 0;
let gift = 10;
ant -= gift;
grasshopper += gift;
print('The Ant has ' + ant + ' seeds, and the Grasshopper has '
+ grasshopper + ' seeds');
JavaScript Concepts: Assignment Operators (+=
, -=
), Binary Expression (+
concatenation)
Grasshopper Concepts: print()