Variable Builder
Continue the basketball game by updating a score variable after a basket.
More variable power
Last lesson, your variables created the teams and scoreboard. Now the game is in progress: use number variables and addition to update the score after a basket.
-
1
Boxes can hold numbers too
let homeScore = 42;has no quotes — that makes it a number, not a string. Numbers can do math: if you writehomeScore + 2, JavaScript answers 44. With quotes,"42"would just be text, like words on a sticker. -
2
Glue pieces together with
+Between strings,
+works like glue. JavaScript opens each box, takes out the value, and sticks all the pieces into one sentence:homeTeam+" now has "+homeScore+" points!"→ "Comets now has 44 points!" -
3
Change one box, update everything
Assignment updates the value inside an existing box:
homeScore = homeScore + shotPoints;. JavaScript reads the old score, adds the shot value, and stores the new score back in the same variable.
Update the basketball score.
Start with the last game's scoreboard, then choose the scoring team and shot value.
homeScore = homeScore + shotPoints;Notice that let creates the score once. The update line changes its stored value.
Updated basketball game
02:18
COMETS
ROCKETS
42
39
3
3
4
◀
Run the update code!
- Run your code to see output.
Type real JavaScript, then run it.
You used addition and assignment to change a score variable during the game.
Try Variable Challenge