During this section, the code we write and learn about will not yet be connected to our game. It’s just for learnings sake!
Comments in Code
Throughout this resource and accompanying CodePens we will use today, you will see what we call
// at the beginning of it:
//this is considered a comment this is not!
The syntax for declaring variables and assigning a value to them is as follows:
var age = 23; // alert(age); var sum = 3 + 4; // alert(sum);
Variables and Calculations
Using the two new pieces of knowledge we’ve acquired, we can combine them to do work like this:
var washCycleMinutes = 38; var dryCycleMinutes = 45; var totalLaundryTime = washCycleMinutes + dryCycleMinutes; alert(totalLaundryTime); // alert message will be: 83
In a new CodePen, declare two variables (name of your choice) and store a value in each one! Declare a third variable that uses both previous variables and a math operation.
Then use the alert method to ensure all values are being stored as expected.
Explore to Learn
One at a time, copy and paste or type in each line of code below into your CodePen - make sure this is in the JS pane. Predict what the output will be, then observe the alert message.
alert(3 < 5)
alert(3 > 5)
alert(3 >= 3)
alert(3 === 5)
alert(5 === 5)
alert(5 !== 5)
Boolean- either true or false - as a response
An if statement sets up different paths that the program can take depending on what is true at a given moment.
Explore: Reading if Statements
- Look at the code in the CodePen below. Jot down your prediction of what will happen when we run this code (once line 5 is uncommented).
- Once you've made your prediction, uncomment line 5 and read the alert. Change the value of the
agevariable to test out other situations!
Try It: Writing if Statements
Follow the prompt in at the bottom of the JS pane in this CodePen. (Same CodePen that was used in previous activity)
Note: You will probably want to comment out the previous example