coe
coe

Reputation: 291

switch statement with multiple values in variable

I am going through the Odin project and trying to learn each concept well before moving on. My expectation for this switch statement would be for the data values inside the variable 'fruit' to trigger the alerts of 'orange', and if I change orange it should trigger the alert for 'pear'.

When I do this the only case that works is for pear, orange is passed over completely. I tried to google how to make this into an array to work but couldn't find any more basic examples. How do I make this work?

fruit = ("orange", "apple", "pear");


switch (fruit) {
    case "orange":
        alert('orange is a fruit')
        break;
    case "hotdog":
        alert('hot dog is meat')
        break;
    case "pear":
        alert('pear is a fruit')
    default:
        document.getElementById('heading').innerHTML = 'NONE';
}

Upvotes: 0

Views: 252

Answers (4)

sonEtLumiere
sonEtLumiere

Reputation: 4572

Declare fruit variable with var/let/const keyword then wrap elements of array between square brackets . Access array elements by his index (fruit[0] equals 'orange')

var fruit = ["orange", "apple", "pear"];
//fruit[0] == orange
//fruit[1] == apple
//fruit[2] == pear

switch (fruit[0]) {
    case "orange":
        alert('orange is a fruit');
        break;
    case "hotdog":
        alert('hot dog is meat');
        break;
    case "pear":
        alert('pear is a fruit');
    default:
        document.getElementById('heading').innerHTML = 'NONE';
}

Upvotes: 0

Rick
Rick

Reputation: 1880

this might be a better way to organize your data:

var data = {
  "orange": "fruit", 
  "apple": "fruit", 
  "pear": "fruit", 
  "hot dog": "meat"
};

console.log(data["orange"]); // fruit
console.log(data["hot dog"]); // meat

Upvotes: 0

Hien Nguyen
Hien Nguyen

Reputation: 18975

The declare with fruit = ("orange", "apple", "pear");

the fruit variable will have value "pear"

You should change it to array as

fruit = ["orange", "apple", "pear"];

for(var i = 0; i < fruit.length; i++){
switch (fruit[i]) {
    case "orange":
        alert('orange is a fruit')
        break;
    case "hotdog":
        alert('hot dog is meat')
        break;
    case "pear":
        alert('pear is a fruit')
    default:
        document.getElementById('heading').innerHTML = 'NONE';
}
}
<div id="heading"></div>

Upvotes: 0

Anthony
Anthony

Reputation: 6482

The Comma Operator is causing your assignment to fruit to be the last operand - in this case 'pear'. If you changed the order, whatever was last would be the value that is stored in fruit.

You could convert fruit to an array and then loop over each item in that array to see the value of the switch change.

var fruits = ["orange", "apple", "pear"];

for(var fruit of fruits) {
  switch (fruit) {
      case "orange":
          console.log('orange is a fruit')
          break;
      case "hotdog":
          console.log('hot dog is meat')
          break;
      case "pear":
          console.log('pear is a fruit')
      default:
          //document.getElementById('heading').innerHTML = 'NONE';
  }
}

(Changed to console.log() to avoid the alert popups in the snippet)

Upvotes: 0

Related Questions