Chapter 1 · Lesson 2 of 5
02. Words, numbers and sums
Text goes in quotes, numbers don't. JavaScript works out sums, and joins text with +.
Text in quotes is a string. Numbers go without quotes, and JavaScript works out sums before it shows them: + - * (times) and / (divide), and % for what's left over after dividing.
console.log(12 + 30);console.log(7 * 6);console.log(10 / 4);console.log(17 % 5);It shows
42 42 2.5 2
Unlike some languages, JavaScript gives 10 / 4 as 2.5: one kind of number for whole numbers and decimals.
+ between strings joins them. console.log can also show several things at once, separated by commas: it puts a space between them.
console.log("Java" + "Script");console.log("Total:", 40 + 2);console.log("5" + 3);console.log(5 + 3);It shows
JavaScript Total: 42 53 8
Your turn
1Steps
- 1A school bus makes 4 trips a day and carries 45 students each trip. Show
Students a day:and the total, worked out by JavaScript, using a comma in console.log. - 2Then show
Left over:and what's left when 100 sweets are shared among 7 friends (use%).
Write it in script.js. If it asks with prompt(), put the answers in Program input, under the terminal.
Use: console.log
Starting code
The editor starts empty. Copy this into it to begin, or write your own from scratch.
console.log("Students a day:");2Expected output
It should show
Students a day: 180 Left over: 2
3Hints
Stuck? Open one hint at a time: each gives a bit more help.
Hint 1
console.log can take two things: the words, a comma, then the sum.
Hint 2
The first line is console.log("Students a day:", 4 * 45);
Hint 3
For the second, 100 % 7 gives what's left over.