SyntaxError: Unexpected token else


#1


Plz help i keep getting
SyntaxError: Unexpected token else



SyntaxError: Unexpected token else


var user = prompt("You suddenly run into a group of ninjas and one of them notices you! Will you FIGHT them, RUN away, or HIDE? (Enter your choice).").toUpperCase();

//-------
switch(user) {
//
case 'FIGHT':
var agile = prompt("Are you agile? (YES or \
NO)").toUpperCase();
var skill = prompt("Are you good with a sword? \
(YES or NO)").toUpperCase();
if(agile === 'YES' && skill === 'YES') {
console.log("You disarm one of \
the ninjas and fight the rest off! You escape!");
} else if (agile === 'YES' && skill === 'NO') { 
console.log("You attempt to disarm one of the ninjas,\
but you trip onf fall on your face.\
The ninjas capture you! You lose!");
} else {
console.log("You reach to grab one of the \
ninjas' swords and attempt to disarm him, but he notices this and cuts your hand off! You lose!");
}
break;
//
case 'RUN':
var run = prompt("Are you quick? \
(YES or NO)").toUpperCase();
var coordination = prompt("Are you well \
coordinated? (YES or NO)").toUpperCase();
if(run === 'YES' || coordination === 'YES') {
console.log("You escape!");
}
break;
} else {
console.log("The ninjas capture you! You lose.");
}
break;
//
case 'HIDE':
var light = prompt("Are you light on your feet? \
(YES or NO)").toUpperCase();
var sneak = prompt("Are you sneaky? \
(YES or NO)").toUpperCase();
if(light === 'YES' || sneak === 'YES') {
console.log("You escape unnotieced!");
if(light === 'YES' || sneak === 'NO') {
console.log("You attempt to hide and step on a pile of sticks. The ninjas find and capture you! You lose.");
if(light === 'NO' || sneak === 'YES') {
console.log("The ninjas see you and eventually capture you! \
You lose!");
} else {
console.log("The ninjas capture you! You lose.");
}
break;
//
default: 
console.log("You escape and start a new quiet life as a farmer. One day you need to go into town to buy some hay for your horses. You are walking through a forest and suddenly run into a group of ninjas! The ninjas capture you!. (Please Enter A Correct Answer");
}


#2

I formatted your code and found the problem: break; was before else (look to my code 'RUN' case). Also problem was with if-else if-else statement in 'HIDE' case.

var user = prompt("You suddenly run into a group of ninjas and one of them notices you! Will you FIGHT them, RUN away, or HIDE? (Enter your choice).").toUpperCase();

switch(user) {
   case 'FIGHT':
      var agile = prompt("Are you agile? (YES or NO)").toUpperCase();
	  var skill = prompt("Are you good with a sword? (YES or NO)").toUpperCase();
	  if(agile === 'YES' && skill === 'YES') {
	  	console.log("You disarm one of the ninjas and fight the rest off! You escape!");
	  } else if (agile === 'YES' && skill === 'NO') { 
	  	console.log("You attempt to disarm one of the ninjas, but you trip onf fall on your face. The ninjas capture you! You lose!");
	  } else {
	  	console.log("You reach to grab one of the ninjas' swords and attempt to disarm him, but he notices this and cuts your hand off! You lose!");
	  }
   break;

   case 'RUN':
      var run = prompt("Are you quick? (YES or NO)").toUpperCase();
      var coordination = prompt("Are you well coordinated? (YES or NO)").toUpperCase();
      if(run === 'YES' || coordination === 'YES') {
         console.log("You escape!");
      } else {  // you had break; before else
         console.log("The ninjas capture you! You lose.");
      }
   break;

   case 'HIDE':
      var light = prompt("Are you light on your feet? (YES or NO)").toUpperCase();
      var sneak = prompt("Are you sneaky? (YES or NO)").toUpperCase();
      if(light === 'YES' || sneak === 'YES') {
         console.log("You escape unnotieced!");
      } else if(light === 'YES' || sneak === 'NO') {
         console.log("You attempt to hide and step on a pile of sticks. The ninjas find and capture you! You lose.");
      } else if(light === 'NO' || sneak === 'YES') {
	  console.log("The ninjas see you and eventually capture you! You lose!");
      } else {
	  console.log("The ninjas capture you! You lose.");
      }
   break;

   default: 
	  console.log("You escape and start a new quiet life as a farmer. One day you need to go into town to buy some hay for your horses. You are walking through a forest and suddenly run into a group of ninjas! The ninjas capture you!. (Please Enter A Correct Answer");
}

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.