Skip to main content

Chapter 6: Sorting Hat Logic

if/else if Trees

The Sorting Hat at Hogwarts considers many factors to decide which house suits a student best. In code, when we have multiple conditions to check, we use an if/else if/else chain, which is like a decision tree. The program will test each condition in order until one is true, then choose that path and ignore the rest.

🎩 Single if vs. Chain of else if

In the previous chapter, we had a single if with an optional else. Now imagine we have more than two houses to consider – four houses, to be precise. We can set up a series of checks:

let trait = "bravery";

if (trait === "bravery") {
console.log("Gryffindor!");
} else if (trait === "loyalty") {
console.log("Hufflepuff!");
} else if (trait === "wisdom") {
console.log("Ravenclaw!");
} else if (trait === "ambition") {
console.log("Slytherin!");
} else {
console.log("Muggle (no magical house)");
}

Here's how this works:

  • The code checks the first if: is trait === "bravery"? If yes, it prints "Gryffindor!" and then stops checking further (the student is sorted, no need to check other houses).
  • If not, it moves to the next condition with else if: is trait === "loyalty"? If yes, print "Hufflepuff!" and stop.
  • It continues down the chain. If none of the if or else if conditions matched (meaning our trait was something else, or maybe not set), then the final else catches it and prints a default message.

Try changing the value of trait in the code above to "loyalty", "wisdom", "ambition", or something like "kindness" (which isn't explicitly handled, so it should hit the else). Each time, only one message will print – the first one whose condition is true.

Order matters: The arrangement of these conditions is important. The code will stop at the first true condition. If two conditions could both be true (not in this specific example, but imagine overlapping ranges or something), whichever comes first wins. So you typically order from most specific to most general conditions.

🏅 Another Example: Grading System

To illustrate further, consider a grading scale:

let score = 85;

if (score >= 90) {
console.log("Grade: A");
} else if (score >= 80) {
console.log("Grade: B");
} else if (score >= 70) {
console.log("Grade: C");
} else if (score >= 60) {
console.log("Grade: D");
} else {
console.log("Grade: F");
}

This logic checks the score against various ranges. If score is 85, the first condition (>=90) is false, then it checks >=80 which is true, so it prints "Grade: B" and stops. Notice we put the highest range first and the lowest last. If we shuffled these around, it could mis-classify scores because the chain would stop too soon on a less specific condition.

⚖️ Using else if vs Separate if Statements

You might wonder, why not just write separate if statements for each house or each grade? For example:

if (trait === "bravery") { ... }
if (trait === "loyalty") { ... }
// ...

If we did that, the program would check all of them, even after finding a match. In some cases that's fine, but often you want a mutually exclusive choice – one and only one of the blocks to execute. The else if chain ensures exclusivity. Once one condition is satisfied, the rest are skipped.

In our grade example, with separate ifs, if you had score = 95 (>=90 true -> print A) it would still check >=80 (which is also true for 95) and print B as well, etc. That would be wrong. The chain prevents that.

🧙‍♂️ Sorting Hat Simulation (Slightly Random)

While trait-based sorting is one approach, the real Sorting Hat considers many things (and sometimes listens to the student's preference). For fun, we could simulate a random house assignment:

let randomNum = Math.floor(Math.random() * 4); // gives 0,1,2 or 3
let houseName;
if (randomNum === 0) {
houseName = "Gryffindor";
} else if (randomNum === 1) {
houseName = "Hufflepuff";
} else if (randomNum === 2) {
houseName = "Ravenclaw";
} else if (randomNum === 3) {
houseName = "Slytherin";
}
console.log("The Sorting Hat has chosen: " + houseName);

We used Math.random() (more on that in a later chapter) to generate a random number from 0 to 3, and then used an if/else-if chain to assign a house. In this scenario, all conditions are checking the same variable (randomNum) against different values. This is a common pattern.

Tip: If you find yourself writing a long chain of else if that all compare the same variable to different constants (like above), another structure exists called switch. It's not inherently better for logic but can sometimes be cleaner for that specific scenario. We won't dive into switch in this book, but feel free to explore it after mastering these basics.

🎭 Try It: Who's That Character?

Try it yourself: Write an if/else if/else chain that outputs a character based on a given nickname or description. For example:

let nickname = "the chosen one";
if (nickname === "the chosen one") {
console.log("Harry Potter");
} else if (nickname === "the brightest witch") {
console.log("Hermione Granger");
} else if (nickname === "the boy who lived") {
console.log("Harry Potter");
} else if (nickname === "he who must not be named") {
console.log("Voldemort");
} else {
console.log("Unknown character");
}

Test it with different nickname values, including ones that hit the else. This is a fun way to see how multiple conditions work.

🌟 Key Points about else if Chains

  • They allow checking multiple conditions in sequence.
  • Only one block from the chain will execute (the first true one).
  • Always end with a plain else to catch any cases not handled (optional but good practice).
  • Use them when you need a clear, one-out-of-many choice in your program's flow.

Now you can create programs that make complex decisions, just like the Sorting Hat! Moving on, we'll explore loops – a way to repeat actions without writing the same code over and over. This will be as if you could cast a charm that causes a spell to repeat itself multiple times (handy, right?). Let's continue our journey into loops.