Lesson 2
Conditional Statements
At their heart, conditional statements are basically this - if this (whatever this is) is true then do this. There are basically three types of conditional statements:
- if
- else
- else if
The thing is, you can't use them independently of the others. You must always have an if statement. You can have if and an else. Or you can have all three. But there will always be an if statement.
Here is the basic if statement.
if (fruit === 'apple') {
console.log('My favorite fruit is an ' + fruit + '.');
}
This would produce the sentence of: "My favorite fruit is an apple." in the console.
If you need to evaluate a variable between two possibilities, you need to use an if..else statement. It's starts out the same.
if (fruit === 'apple') {
console.log('My favorite fruit is an ' + fruit + '.');
} else {
console.log('You input ' + fruit + '. That's not my favorite. I like apples.');
}
This would produce the sentence of: "You input peach. That's not my favorite. I like apples." in the console.
Lastly, if you need to a third comparison (or more) in your statement, you'll need an else if added in there.
if (fruit === 'apple') {
console.log('My favorite fruit is an ' + fruit + '.');
} else if (fruit === 'pear') {
console.log('Pears are good, but I prefer apples.');
} else {
console.log('You input ' + fruit + '. That's not my favorite. I like apples.');
}
The Switch Statement
If you need to run a variable through various comparisons, you could use an if, else if, else if, else if, else if,(on so on) ending with an else. But all the brackets and parentheses can get pretty tiresome. A switch statement jumps in for the rescue. They are much easier to code and are really useful when you have a bunch of conditions under which your variable could fall.
Here is an example of a switch statement for possible reasons a cake didn't rise.
var cakeFlop = 'did not rise';
switch (cakeFlop) {
case 'top domed':
console.log('A domed top means the outside of the cake
cooked faster than the middle. To prevent this, try
wrapping the outside of the pan in crinkled foil to
slow down the cooking on the edges.');
break;
case 'did not rise':
console.log('A cake not rising can be due to several
reasons. Two most common are old ingredients or the
batter sat around too long.');
break;
case 'will not release':
console.log('Cakes release best while still warm, but
not so warm that it is still fragile. The optimal
window for release is 10 - 15 minutes. If you have
waited too long, try warming the cake pan up in the oven
for a few minutes.');
break;
default:
console.log('Hmmm. I do not have a response for that
problem. Try searching for it in Google. Google knows
everything.');
break;
}
Operators
Okay, most of the operators are pretty straight forward. These are much the same as they are for Math Operators. It's just with conditional operators you are comparing a value against a condition. But there is one thing you have to remember about the equal sign.
One equal sign (=) is used to set or change the value of a variable.
Three equal signs (===) are used to check to see whether or not a variable is equal to something else, such as in an if...else statement.
if (fruit === 'apple') {
console.log('Apples are great.');
}else {
console.log('What? You don\'t like apples.');
}
Activities
- Complete Conditionals in Codecademy. There are 11 exercises. Pay attention to your syntax (all of the brackets, parentheses, and semicolons). They make or break your coding. Also, pay attention to the sentences Codecademy wants you to type. It's a computer looking at your work. A computer sees 'My favorite food is pizza.' and 'my favorite food is pizza.' as two different things. So if your code is not working and you've checked the syntax, try checking your spelling.
- Show Mrs. Gilmore your checkmark in Codecademy for Conditionals.
- Complete the checkpoint that will be posted in Google Classroom.
Resources
- Codecademy
- Online Console
- Debugger: Remeber to include the following at the top of your coding to avoid the warnings about let and const.
/*jshint esversion: 6 */