Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Quiz: Basic if
What will this code print?
let hp = 60;
if (hp > 50) {
console.log("Strong");
}Strongtrue60Hint : Evaluate the condition with the value 60.
Exercise 2 — Quiz: Valid Syntax
Which if statement is written correctly?
if (level > 10) { console.log("OK"); }if level > 10 { console.log("OK"); }if (level > 10) console.log("OK") }if [level > 10] { console.log("OK"); }Hint : Compare the four options character by character: the type of brackets used, and where each one opens and closes.
Exercise 3 — Quiz: if/else Output
What will this code print?
let energy = 30;
if (energy >= 30) {
console.log("Attack");
} else {
console.log("Rest");
}AttackRestAttack then RestHint : Look carefully at which comparison operator is used, then test it with the exact value of the variable.
Exercise 4 — Quiz: Chain Result
What will this code print?
let level = 35;
if (level >= 50) {
console.log("Master");
} else if (level >= 30) {
console.log("Advanced");
} else if (level >= 10) {
console.log("Intermediate");
} else {
console.log("Beginner");
}AdvancedIntermediateAdvanced and IntermediateMasterHint : Go from the top and stop at the first true condition.
Exercise 5 — Quiz: Ternary Value
What is stored in result ?
let score = 85;
let result = score >= 80 ? "win" : "lose";"win""lose"true85Hint : Evaluate the condition first. Then recall which of the two values a ternary picks in that case.
Exercise 6 — Quiz: switch Match
What will this code print?
let type = "plant";
switch (type) {
case "fire":
console.log("Pyrochu");
break;
case "plant":
console.log("Florachu");
break;
default:
console.log("Unknown");
}FlorachuPyrochuUnknownFlorachu then UnknownHint : Compare the value of the variable with each case label, one by one.
Exercise 7 — Quiz: Falsy Condition
What will this code print?
let potionCount = 0;
if (potionCount) {
console.log("Drink a potion");
} else {
console.log("No potions left!");
}No potions left!Drink a potion0Hint : The condition here is not a comparison, it's a plain number. Think back to the truthy and falsy chapter.
Exercise 8 — Quiz: else Rules
Which statement about else is true ?
if condition is falseif before itHint : Think about when the else branch ran in the examples you tried, and whether you ever wrote a condition next to it.
Exercise 9 — Quiz: Combined Condition
What will this code print?
let level = 18;
let hasStone = false;
if (level >= 15 || hasStone) {
console.log("Evolution possible");
} else {
console.log("Not yet");
}Evolution possibleNot yetfalseHint : Evaluate each side of the condition separately, then apply the rule of the operator that joins them.
Exercise 10 — Quiz: Wrong Order
What will this code print?
let xp = 500;
if (xp >= 100) {
console.log("Level 2");
} else if (xp >= 300) {
console.log("Level 3");
} else if (xp >= 500) {
console.log("Level 4");
}Level 2Level 4Level 3Hint : Test the conditions in the order they are written, and remember how many branches of a chain can run.
Exercise 11 — Quiz: Ternary Structure
In a ternary expression, what does the : separate?
Hint : Picture a full ternary and locate where each of its three pieces sits relative to the :.
Exercise 12 — Quiz: Fall-through
What happens in a switch when a matching case has no break ?
break is founddefault runsHint : Think back to what break is for in a switch. Then ask what happens without it.
Exercise 13 — Quiz: Two Independent ifs
What will this code print?
let hp = 100;
if (hp >= 50) {
console.log("A");
}
if (hp >= 80) {
console.log("B");
}A then BABHint : Look at the keyword that starts the second block: is it connected to the first one or not?
Exercise 14 — Quiz: elif Doesn't Exist
How do you write the "middle" branch of a condition chain in JavaScript?
else if (condition) { ... }elif (condition) { ... }elseif (condition) { ... }else-if (condition) { ... }Hint : Some of these options come from other languages. Reread how the middle branch was written in the lesson.
Exercise 15 — Quiz: Ternary Output
What will this code print?
let isShiny = false;
console.log(isShiny ? "Rare Codémon!" : "Classic Codémon");Classic CodémonRare Codémon!falseconsole.logHint : A ternary produces a value, so it can live anywhere a value fits. Which side is picked when the condition is false ?
Exercise 16 — Quiz: No Match
What will this code print?
let city = "Lavaville";
switch (city) {
case "Aquacity":
console.log("Water gym");
break;
case "Floratown":
console.log("Plant gym");
break;
}
console.log("Trip over");Trip overdefault is requiredWater gymHint : Compare the value with every case, then notice what this switch does not have at the end. The last line sits outside the switch.
Exercise 17 — Quiz: The Sneaky Semicolon
What will this code print?
let hp = 100;
if (hp < 10); {
console.log("Danger!");
}Danger! even though HP is 100falseHint : Read the first line very slowly, character by character, and ask what the if actually controls.
Exercise 18 — Quiz: switch and Types
What will this code print?
let door = "1";
switch (door) {
case 1:
console.log("Number door");
break;
default:
console.log("No match");
}No matchNumber doorHint : Look closely at the type of the variable and the type of the case label. How strict is a switch when it compares?
Exercise 19 — Quiz: AND Condition
What will this code print?
let level = 22;
let badges = 2;
if (level > 20 && badges >= 3) {
console.log("Championship unlocked");
} else {
console.log("Keep training");
}Keep trainingChampionship unlockedHint : Check each side of the condition on its own with the given values before combining them.
Exercise 20 — Quiz: Choose Your Weapon
You need to display a different message for each HP range (0-20, 21-50, 51-100). What's the best tool?
else if chainswitchHint : Ask yourself what each tool can compare: exact values only, or something more flexible?
Exercise 21 — Quiz: Nothing True
What will this code print?
let energy = 5;
if (energy >= 80) {
console.log("Mega attack");
} else if (energy >= 40) {
console.log("Normal attack");
} else if (energy >= 10) {
console.log("Small attack");
}Small attackMega attackHint : Check each condition with the value 5, then look at how the chain ends.
Exercise 22 — Quiz: Ternary to if/else
Which if/else is equivalent to let mode = isNight ? "sleep" : "train"; ?
let mode; if (isNight) { mode = "sleep"; } else { mode = "train"; }let mode; if (isNight) { mode = "train"; } else { mode = "sleep"; }let mode; if (mode) { isNight = "sleep"; } else { isNight = "train"; }let mode = "sleep" if isNight else "train";Hint : Map the three parts of the ternary onto the three parts of an if/else, in order.
Exercise 23 — Quiz: Role of default
In a switch, default plays the same role as which keyword in an if chain?
elseelse ififbreakHint : When does default run? Find the keyword in an if chain that runs at the same moment.
Exercise 24 — Quiz: Truthy String
What will this code print?
let nickname = "Zappy";
if (nickname) {
console.log("Hello " + nickname + "!");
} else {
console.log("No nickname yet");
}Hello Zappy!No nickname yettrueHint : The condition is a string, not a comparison. Think back to how JavaScript treats a string in a condition.
Exercise 25 — Quiz: Full Combo
What will this code print?
let hp = 35;
let hasPotion = true;
let status = hp < 40 ? "weak" : "fine";
if (status === "weak" && hasPotion) {
console.log("Drinking potion!");
} else if (status === "weak") {
console.log("Find a healing center!");
} else {
console.log("All good!");
}Drinking potion!Find a healing center!All good!weakHint : First resolve the ternary to know status, then walk the chain from the top.