Essential Question: How can I write an if statement?
Mastery Objectives:
SWBAT create conditional statements.
Do Now: Write a JavaScript program to determine whether a given year is a leap year in the Gregorian calendar. Check the following years: 2016, 2000, 1700, 1800, and 100. Check if next year is a leap year. Now check if the year you were born is a leap year.
Directions: Read below and type the following code:
Using the let keyword, declare a variable named sale. Assign the value true to it.
Checkpoint 2 Passed
Now create an if statement. Provide the if statement a condition of sale.
Inside the code block of the if statement, console.log() the string 'Time to buy!'.
Checkpoint 3 Passed
Notice that the code inside the if statement ran, since 'Time to buy!' was logged to the console.
Below the sale variable declaration, but before the if statement, reassign sale to false. Run your code and observe what happens, we’ll be changing this behavior in the next exercise.
Add an else statement to the existing if statement. Inside the code block of the else statement, console.log() the string 'Time to wait for a sale.'
Using let, create a variable named hungerLevel and set it equal to 7.
Write an if...else statement using a comparison operator. The condition should check if hungerLevel is greater than 7. If so, the conditional statement should log, 'Time to eat!'. Otherwise, it should log 'We can eat later!'.
After you press run, play around with the condition by tweaking the comparison of hungerLevel by using different operators such as <=,>=,>, and <.
In order for this conditional statement to work, you need to declare the variables stopLight and pedestrians and you need to give them a value.
let mood = 'sleepy';
let tirednessLevel = 6;
there are two variables mood and tirednessLevel.
Let’s create an if...else statement that checks if mood is 'sleepy' and tirednessLevel is greater than 8.
If both conditions evaluate to true, then console.log() the string 'time to sleep'. Otherwise, we should console.log() 'not bed time yet'.
After you press “Run”, play around with the || operator and the ! operator! What happens if you negate the value of the entire statement with ! and switch to || instead of &&?
Truthy and Falsey
Change the value of wordCount so that it is truthy. This value should still be a number.
After you make this change and run your code, 'Great! You've started your work!' should log to the console.
Change the value of favoritePhrase so that it is still a string but falsy.
After you make this change and run your code, 'This string is definitely empty.' should log to the console.
Let’s use short-circuit evaluation to assign a value to writingUtensil. Do not edit tool yet, we’ll return to tool in the next step.
Assign to writingUtensil the value of tool and if tool is falsy, assign a default value of 'pen'.
Notice that text 'The pen is mightier than the sword' logged to the console. Which means the value of writingUtensil is 'pen'.
What if we reassign the value of tool to 'marker'. Let’s see what happens to the value of writingUtensil.