Back to Real Variables
Code Studio · Level 2

Variable Builder

Continue the basketball game by updating a score variable after a basket.

20-25 min Ages 9-12 Builder JavaScript

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. 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 write homeScore + 2, JavaScript answers 44. With quotes, "42" would just be text, like words on a sticker.

  2. 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. 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.

Code Editor

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.

Output

Updated basketball game

Basketball scoreboard 02:18 COMETS ROCKETS 42 39 3 3 4
Basketball hoop Basketball Run the update code!

Type real JavaScript, then run it.