Manoj Kumar
Manoj Kumar

Reputation: 61

Understanding how the switch statement works in JS - am i missing something?

My understanding of the switch statement in general is that, we have what we term as a 'switch value' and each 'case' in a switch statement indicates what code we run if we find a match with that switch value.

However, observe the code below and explain to me why its invalid/incorrect logic wrt using switch statement:

function getScore() {
    var score = prompt("Enter student score: ");
    var score = parseInt(score);
    showGrade(score);
}

function showGrade(score) {
    var grade_bands = ["1st Class", "2nd Class", "3rd Class", "Pass", "Referal", "Fail/Re-Take"];
    switch (score) {
    case score >= 70:
            console.log("Your grade band is " + grade_bands[0]);
            break;
        case score >= 60:
            console.log("Your grade band is " + grade_bands[1]);
            break;
        case score >= 50:
            console.log("Your grade band is " + grade_bands[2]);
            break;
        case score >= 40:
            console.log("Your grade band is " + grade_bands[3]);
            break;
        case score >= 30:
            console.log("Your grade band is " + grade_bands[4]);
            break;
        case score >= 20:
            console.log("Your grade band is " + grade_bands[5]);
            break;
    }

}


getScore();

The above yields 'undefined' when observed in console.

However now look at the code below if you may, and explain why when we use a bool 'true' inside switch(), the code works:

function getScore() {
    var score = prompt("Enter student score: ");
    var score = parseInt(score);
    showGrade(score);
}

function showGrade(score) {
    var grade_bands = ["1st Class", "2nd Class", "3rd Class", "Pass", "Referal", "Fail/Re-Take"];
    switch (true) {
    case score >= 70:
            console.log("Your grade band is " + grade_bands[0]);
            break;
        case score >= 60:
            console.log("Your grade band is " + grade_bands[1]);
            break;
        case score >= 50:
            console.log("Your grade band is " + grade_bands[2]);
            break;
        case score >= 40:
            console.log("Your grade band is " + grade_bands[3]);
            break;
        case score >= 30:
            console.log("Your grade band is " + grade_bands[4]);
            break;
        case score >= 20:
            console.log("Your grade band is " + grade_bands[5]);
            break;
    }

}


getScore();

Some clarification of where I am over thinking/ under thinking or simply 'not thinking might help here folks.

** UPDATE **

So here is mplungjans code modified so that it takes the value of grade and uses it as an index to output the according 'band' that student obtained. Notice my boolean logic differs to his though. His logic gives incorrect bands as per score. Mine below gives correct band as per score:

var score = prompt("Please enter your score: ");
var score = parseInt(score);
var grade = "";

if (score >= 70) grade = 5;
  else if (score >= 60) grade = 4;
  else if (score >= 50) grade = 3;
  else if (score >= 40) grade = 2;
  else if (score >= 30) grade = 1;
  else  grade = 0;
  var grade_bands = ["Fail/Re-Take","Referal","Pass","3rd Class","2nd Class","1st Class"];

console.log("You obtained a " + grade_bands[grade] + " band");

Upvotes: 0

Views: 62

Answers (1)

mplungjan
mplungjan

Reputation: 178403

Your switch(true) works but I call it an abuse of the switch since it will take ANY statement that return truthy or falsy

You ACTUALLY want to use a switch against specific values.

You CAN do it like this, but IFs in your case would work MUCH better

const grade_bands = ["1st Class", "2nd Class", "3rd Class", "Pass", "Referal", "Fail/Re-Take"];

function showGrade(score) {
  switch (score) {
    case 10:
    case 11:
    ...
    case 20:
    console.log("Your grade band is " + grade_bands[5]);
    break;
    case 30:
    console.log("Your grade band is " + grade_bands[4]);
    break;
    ....
  }

}

IFs

  if (score <20) grade = 0
  else if (score <30) grade = 1
  else if (score <40) grade = 2
  else if (score <50) grade = 3
  else if (score <60) grade = 4
  else  grade = 5
  return  ["Fail/Re-Take","Referal","Pass","3rd Class","2nd Class","1st Class"][grade] 

Upvotes: 3

Related Questions