let age = 16;
let access;
if (age >= 0 && age <= 12) {access = "Access to G-rated movies only";}
else if (age >= 13 && age <=16) {access = "Access to PG-13 movies";}
else if (age >= 17 && age <= 20) {access = "Access to R-rated movies";}
else {access= "Access to all movie ratings";}
let hoursSlept = 6.5;
let sleepQuality;
if ( hoursSlept < 5) {sleepQuality = "Poor Sleep";}
else if (hoursSlept >= 5 && hoursSlept <= 6.9) {sleepQuality = "Fair sleep";}
else if ( hoursSlept >= 7 && hoursSlept <= 8.5) {sleepQuality = "Good sleep";}
else { sleepQuality = "Oversleeping";}
let age = 17;
if (age >= 18) {
"They are allowed to vote.";
} else {
"They aren't allowed to vote.";
}
let temp = 28;
if (temp < 32) {
console.log("It is freezing.");
} else {
console.log("It's not freezing.");
}
(This is logging it)
or
let temp = 28;
let message;
if (temp < 32) {
message = "It is freezing.";
} else {
message = "It's not freezing.";
}
(This assigning it. The message variable is assigning the statement in the blocks, but you can still log it in the console.)
I asked Copilot for some if else exercises and at first it gave me the double value or ampersand exercises to solve, and then I asked it for single value, and it gave me single value exercises to solve. I practiced both. Are these correct the way I practiced the exercises Hayk or is there something I missed or got wrong?
I also want to share what I learned from ai was that, you should either log or assign in the if else statement blocks so JavaScript can do something with the statement like log it, assign it, show what is being printed.
Ai also told me that I could assign a variable in the if else blocks without declaring it first , but if I did that it would make that variable a global variable and it would cause bugs and issues and it better to declare the variable after the condition variable before writing your if else statements to make the code more predictable and cleaner, like in the first two examples I practiced with age and hoursSlept.
Hayk, did I do the exercises with the if else if statements? Is everything the ai told me correct?