IDK why this isn't working

javascript

#1


I am making a game with my friend, and I keep running across a problem. Whenever I run the code,it asks the question and uses the default switch statement instead of the one it could use. I commented out all of the variables that I am not using yet. Plz help me find out what is wrong :slight_smile:


//var bag = [];

//var enemyName = ["Fill this in with a name", "This one too", "This one", "and this one"];

//var fiftyPercentChance = Math.floor(Math.randon() * 2 + 1);

//var enemyEncountered = function() {
//prompt("You encountered a wild" + " " + enemyName[] + "! Do you ''Run'', //''Fight'', or check your ''Bag''?");
//if (enemyEncountered = "Run") {
//  if (fiftyPercentChance === 2) {
//    console.log("You got away successfully");
//  } else {
//    console.log(enemyName[] + " won't let you do that");
//  }
//} else if (enemyEncountered = "Bag") {
//if (bag === "") {
//confirm("You don't have any items in your bag!")
//}
//console.log ("Chat the item you wish to use:" + bag + "!")
//}
//};

var magicType = function() {
  prompt("Choose your magic: ''Earth'', ''Fire'', ''Ice'', or ''Lighning''");
};
magicType();
switch(magicType){
  case 'Earth':
    confirm("You chose the Earth magic type");
    break;
  case 'Fire':
    confirm("You chose the Fire magic type");
    break;
  case 'Ice':
    confirm("You chose the Ice magic type");
    break;
  case 'Lightning':
    confirm("You chose the Lighning magic type");
    break;
  default:
    confirm("Choose a magic type please!");
    break;
}


#2

you need to store the result of the prompt:

store = prompt("Choose your magic: ''Earth'', ''Fire'', ''Ice'', or ''Lighning''");

and then pass that in the case/switch statement:

switch(store){

magicType is the function, it will never equal a certain string


#3

Hey @ragingamer333 - no need to rage :stuck_out_tongue:

prompt itself is a function. So try it this way:

var magicType = prompt("Choose your magic: 'Earth', 'Fire', 'Ice', or 'Lighning'");
switch(magicType) 
// etc ...

Hope this will help :slight_smile:


#4

removing the function altogether might indeed be a better idea. But that depends on the plans of the user.


#5

True, but I'll assume that it's just a simple Input :smile:


#6

That really did solve the problem! Thanks!