Lesson 2: Numbers
Variables can store different types of information. In the last exercise, you used a variable to store a piece of text (a string). This time, you’ll create a variable to store a number. Numbers in JavaScript don’t need special symbols like quotation marks. You just write the number as it is.
Here is an example of a number variable in JavaScript that stores the number 10. Hover over each part of the code to see what it does.
Write a line of code to declare a variable called delay and set it to a number of your choice. This number will indicate how frequently the Sphero ball will change color. The number should be in milliseconds. For example, if you want the ball to change color every 2 seconds, you would set delay to 2000.
If you're having trouble with this exercise, here are some common mistakes and hints to help you out:
| Mistake | Hint |
|---|---|
| Using quotes around the number, like var delay = "2000"; | Numbers should not have quotes. Remove them to make delay a number instead of a string. |
| Forgetting to use var when declaring the variable, like delay = 2000; | Always use var when declaring a variable for the first time. You might find other resources telling you to use let or const but we need to use var for the automated system to work. |
| Misspelling the variable name like var dealy = 100 | Check for typos! Variable names must match exactly. |
| Using a decimal value instead of a whole number, like var delay = 2000.5; | Milliseconds should be whole numbers. Use a number like 2000 instead of a decimal. |
| Using a negative number, like var delay = -500; | Time delays can't be negative. Choose a positive number instead. |