Day 4
Doing if…else statements. The logic is simple but getting used to the syntax of the code is the tricky bit.
let sale = true;
sale = false;
if(sale) {
console.log(‘Time to buy!’);
} else {
console.log(“Time to wait for a sale.”);
}
Looking at comparison operators now. I don’t like === and !==. I would prefer just == and !=. Why is it like this?
//Should I eat?
let hungerLevel = 7;
if (hungerLevel > 7){
console.log(“Time to eat!”);
} else {
console.log(“I can eat later!”);
}
Next onto logical operators, this reminds me of when I studied electronics in school; looking at logic gates.
//Should I sleep?
let mood = ‘sleepy’;
let tirednessLevel = 6;
if (mood === “sleepy” && tirednessLevel > 8){
console.log(“time to sleep”);
} else {
console.log(“not bed time yet”);
}
Truthy and falsy; funny words! Doing short-circuit evaluations and it’s hurting my head a little trying to translate the long way of doing it into the short way:
let tool = ‘marker’;
let writingUtensil = tool || “pen”;
console.log(
The ${writingUtensil} is mightier than the sword.
);
I am starting to get it, I think the course just does a poor job of explaining what’s going on.
I like the look of ternary operators; anything to squeeze typing down to a minimum and keep things looking clean!
let isLocked = false;
isLocked ? console.log(‘You will need a key to open the door.’) : console.log(‘You will not need a key to open the door.’);let isCorrect = true;
isCorrect ? console.log(‘Correct!’) : console.log(‘Incorrect!’);let favoritePhrase = ‘Love That!’;
favoritePhrase === ‘Love That!’ ? console.log(‘I love that!’) : console.log(“I don’t love that!”);
Next, else if statements, for more complex conditionals.
let season = “summer”;
if (season === “spring”) {
console.log(“It’s spring! The trees are budding!”);
} else if(season === “winter”) {
console.log(“It’s winter! Everything is covered in snow.”);
} else if(season === “fall”) {
console.log(“It’s fall! Leaves are falling!”);
} else if(season === “summer”) {
console.log(“It’s sunny and warm because it’s summer!”);
} else {
console.log(“Invalid season.”);
}
Finally I end today’s session on Switch statements.
let athleteFinalPosition = ‘first place’;
switch (athleteFinalPosition) {
case “first place”:
console.log(“You get the gold medal!”)
break;
case “second place”:
console.log(“You get the silver medal!”)
break;
case “third place”:
console.log(“You get the bronze medal!”)
break;
default:
console.log(“No medal awarded.”)
break;
}
I found today quite tough compared to the previous three. I’m not feeling so confident I can remember everything as I have done thus far.