Operators

Let's do some arithmetic. Jake, Sally, and Ted sell apples at the local farmers market. They have a total of 639 apples, and each person sets up a stand at different location. Let's see how their day at the farmers market goes!

Make sure to complete your work in the console. Even if you can do the math in your head, try it in JavaScript.

  1. I've created five variables for you to keep track of Jack, Sally, and Ted's apple business - totalApples, jakesApples, sallysApples, tedsApples and soldApples. Print out each variable in the console to see what it's current value is.
  2. Jake, Sally, and Ted need to split up the apples equally. Assign one third of the totalApples to jakesApples, sallysApples and tedsApples variables using the division operator.
    • Example: jakesApples = totalApples / 3;
  3. Sally makes the first sale. She sells 3 apples. Subtract 3 from sallysApples to keep track.
  4. Ted makes the second sale. He sells one dozen apples. Subtract 12 from tedsApples to keep track.
  5. Jake sells to a local business who buys seven dozen apples. Subtract seven dozen apples from jakesApples.
    • Hint: x - (7 * 12)
  6. Sally sells apples to a family of 5, with each person receiving 3 apples. Update her total using the subtraction and multiplication operator. Use parenthesis to indicate which part of the equation should be evaluated first.*
    • Hint: Parenthesis are not needed with the assignment shorthand operators (i.e. -= x * y).
  7. Sally sells fifteen dozen apples to a local grocery store. Update her total using the subtraction and multiplication operators.*
  8. Ted and Jake each sell 100 more apples to finish out the day. Update their totals.*
  9. Find out how many apples Jake, Sally, and Ted sold by subtracting the number of apples each person has left from totalApples. Assign that value to soldApples.
    • Hint: You can do this on one line: soldApples = totalApples - (jakesApples + ...)
  10. Check your work by running validateApples(totalApples, soldApples, jakesApples, sallysApples, tedsApples);.

* Only use the shorthand assignment operators on these tasks.


Extra Credit

  1. Each apple is sold for $1. Jake, Sally, and Ted split the amount equally, except they don't make change. They give the remainder to whoever sold the most apples. Find out the remainder of the total revenue using the modulus operator.