How to change global variable with a switch statement JavaScript?

How to change global variable with a switch statement JavaScript?
0

#1

Hey guys,

I am working on some simple JS games to practice my JavaScript knowledge. Ran into a bit of a roadblock. I want to make the variable band equal the result of the switch statement. I had originally had the case do (band = “The Twisters”) instead of using return but I don’t believe that worked.

Any help would be super awesome!!!

var band = "";

var bandName = function(){
  var bandNum = (Math.floor(Math.random() * (10 - 1)) + 1);
  switch(bandNum) {
  case 1:
    return "The Twisters";
  case 2:
    return "School Of Rock";
  case 3:
    return "Yum";
    break;
  case 4:
    return "Slayer";
  case 5:
    return "My Chemical Romance";
  case 6:
    return "Nsync";
  case 7:
    return "Goat";
  case 8:
    return "Gwar";
  case 9:
    return "No Vacancy";
  case 10:
    return "Knocked Loose";

}

};

#2

As your function is returning a value, when you call bandName() call it like so:

band = bandName();

That update the value of band to that returned by the function.