To get the game working we're going to need both "if-else" statements to make decisions as well as a way to keep track of data. In this challenge, we're going to learn how to use variables to keep track of data. Think of a variable like a labeled box that stores information that you can pull up later. Once you have a good handle on how to work with variables, you'll be in good shape to start building the game. We're using a brand-new starting point here, so make sure to click the button below.
First, open up console by pressing the "f12" key on your computer, or by right clicking on your browser window, clicking "Inspect" and clicking the "console" menu item. We'll start adding the code at the top of console window next to the blue ">"
Type out "var counter = 0;" and hit enter. You'll see an "undefined" like in the image below because this command doesn't output any information, it just stores information.
This line of code is doing several things:
"var" lets the browser know you're setting a new variable.
"counter" is the name of the variable. You could have called it anything you want, as long as the spelling you use in your code is consistent.
the "=" separates the name of the variable from what you want to set as it's value. The equals sign in code is very different from how you might have seen it in math class. It points to what goes into a variable, it doesn't check whether two values are equal.
Whatever is on the right side of the "=" becomes what the variable stores. In this case, the variable stores the value 0
The ";" tells the browser the statement is done
If you type out the name of the variable, "counter", you should see the value of the variable in the output.
Anything to right of the "=" becomes the new value inside the variable. You might not have guessed it, but you can actually use the variable itself on the right side of the "=" with something like "counter = counter + 3;".
If the initial value of counter is 1, the "counter + 3" becomes 4. That's the new value of "counter", so the statement breaks down to something like: