Quizz

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Quiz: Basic if

What will this code print?

let hp = 60; if (hp > 50) { console.log("Strong"); }
Options
• Strong
• Nothing at all
• true
• 60

Hint : Evaluate the condition with the value 60.

Exercise 2 — Quiz: Valid Syntax

Which if statement is written correctly?

Options
• 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"); }
Options
• Attack
• Rest
• Attack then Rest
• Nothing at all

Hint : 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"); }
Options
• Advanced
• Intermediate
• Advanced and Intermediate
• Master

Hint : 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";
Options
• "win"
• "lose"
• true
• 85

Hint : 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"); }
Options
• Florachu
• Pyrochu
• Unknown
• Florachu then Unknown

Hint : 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!"); }
Options
• No potions left!
• Drink a potion
• An error, the condition is not a boolean
• 0

Hint : 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 ?

Options
• It runs only when the if condition is false
• It needs its own condition in parentheses
• It can exist without an if before it
• It always runs, no matter what

Hint : 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"); }
Options
• Evolution possible
• Not yet
• Nothing, the condition is invalid
• false

Hint : 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"); }
Options
• Level 2
• Level 4
• Level 3
• All three levels

Hint : 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?

Options
• The "true" value from the "false" value
• The condition from the "true" value
• Two independent conditions
• The variable name from its value

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 ?

Options
• The next cases also run, until a break is found
• The switch stops automatically after the case
• JavaScript throws an error
• Only default runs

Hint : 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"); }
Options
• A then B
• Only A
• Only B
• Nothing at all

Hint : 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?

Options
• 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");
Options
• Classic Codémon
• Rare Codémon!
• false
• An error, a ternary can't be inside console.log

Hint : 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");
Options
• Only Trip over
• An error, default is required
• Water gym
• Nothing at all

Hint : 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!"); }
Options
• Danger! even though HP is 100
• Nothing, the condition is false
• A syntax error
• false

Hint : 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"); }
Options
• No match
• Number door
• An error
• Nothing at all

Hint : 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"); }
Options
• Keep training
• Championship unlocked
• Both messages
• Nothing at all

Hint : 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?

Options
• An else if chain
• A switch
• A single ternary
• Three separate variables

Hint : 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"); }
Options
• Nothing at all
• Small attack
• An error
• Mega attack

Hint : 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"; ?

Options
• 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?

Options
• The final else
• else if
• if
• break

Hint : 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"); }
Options
• Hello Zappy!
• No nickname yet
• An error, a string is not a condition
• true

Hint : 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!"); }
Options
• Drinking potion!
• Find a healing center!
• All good!
• weak

Hint : First resolve the ternary to know status, then walk the chain from the top.

Back to the course