Lesson 3 of 14 · 7 min

Numbers and text

JavaScript does arithmetic the way you expect:

Symbol Meaning Example Result
+ add 7 + 3 10
- subtract 7 - 3 4
* multiply 7 * 3 21
/ divide 7 / 2 3.5
% remainder 7 % 3 1

Brackets work as in maths: (2 + 3) * 4 is 20.

Building sentences with + gets messy. Template literals are neater. They use backticks (`, usually on the key left of 1) and put values inside ${ }:

const item = "bread";
const price = 1.5;
console.log(`One ${item} costs ${price} dollars.`);
// One bread costs 1.5 dollars.

Anything inside ${ } is worked out first, so you can even write ${price * 2}.

Your task

The starter has a price and a quantity.

  1. Create total: the price times the quantity.
  2. Create perPerson: the total shared between 4 people.
  3. Print exactly 3 meals cost 750, so 187.5 each using a template literal and your variables.
main.js
const price = 250;
const quantity = 3;

// Work out total and perPerson, then print the sentence.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…