What is wrong with my code


#1

Hi everyone, I keep getting SyntaxError: Unexpected token var in the code below, can you tell me how to fix this and any other problems thank you very much.
var user = prompt("your walking the woods with sword, and then a few yards away an oger steps onto the trail holding a club. do you fight him, give him a riddle, or scare him").toUpperCase();

switch(user) {
case 'fight'
var strength = prompt('are you moderately strong(yes or no)').toLowerCase();
var speed = prompt('are you fast (yes or no)').toLowerCase();
if(strength && speed === 'yes') {
console.log("you run and cut the orgers club at the handle The oger barely sees you rush by and is confused to see his club lying on the ground, giving you just engnough time to get away");
}
if else (speed === 'no' && strength === 'yes' || 'no') {
console.log("to slow you cut the ogers club, then he knocks you unconsis with a strong punch; then walks away");
else {
console.log("your speed and swords sharpness are engough you get away"); }
break;
case 'riddle'
var round = prompt('do you tell him what round as the moon but gold like the the sun(yes or no)').toLowerCase();
var what = prompt('do you ask what do you get when you cross a retorical question and a joke(yes or no)').toLowerCase();
if(round === 'yes' || what === 'no') {
console.log('The oger looks up at the sun, then says the sun, you say aa no a wedding ring, the oger says not my wedding ring, you get whomped');
}
if else(round === 'yes' || what === 'yes') {
console.log("The oger looks up at the sun, then says the sun, you then say what do you get when cross a retorical question with a joke the oger sits down to think and doesn't notice you tiptoeing away");
}
else {
console.log("The oger sits down to think and doesn't notice you walking away(the answer is a riddle)");
}
break;
case 'scare him'
console.log("you shrunch up your face, raise your hands then make a hidious noise he is terified. If you had paid attention in school you would have know that ogers are to stupid to run away, you get whomped");
break;
default:
console.log("I don't understand but you would probably get whomped");

}

#2

The error message you are seeing is caused by this line:

case 'fight'
    var strength = ...

case is expected to include a colon : before the code block associated with it.

case 'fight': 
    var strength = ...

This would apply to all the cases in your code, as none have this token.


#3

Logical expression review:

if else (speed === 'no' && strength === 'yes' || 'no') {
  1. It should read, else if
  2. speed === 'no' && strength === 'yes' || strength === 'no'

'no' is a string with length > 0 so will convert to a Boolean true in your code above. This line treats it as an identity expression so there is no false positive.

Expressions are read from left to right, so we can see that the above will need some grouping to tell JavaScript to read it as two operands around &&.

speed === 'no' && (strength === 'yes' || strength === 'no')

#4

Thanks a million, this was a big help. also on the if else (speed === 'no' && strength === 'yes' || 'no') {
console.log("to slow you cut the ogers club, then he knocks you unconsis with a strong punch; then walks away"); line I needed to add a } , then it would pass. Their were also a few other thing I need to do to make program work the way i wanted. In case your interested here's the full finished program.
var user = prompt("your walking the woods with sword, and then a few yards away an oger steps onto the trail holding a club. do you fight him, give him a riddle, or scare him").toLowerCase();

switch(user) {
case 'fight':
var strength = prompt('are you moderately strong(yes or no)').toLowerCase();
var speed = prompt('are you fast (yes or no)').toLowerCase();
if(strength === 'yes' && speed === 'yes') {
console.log("you run and cut the orgers club at the handle The oger barely sees you rush by and is confused to see his club lying on the ground, giving you just engnough time to get away");
}
else if (speed === 'no' && (strength === 'yes' || strength === 'no')) {
console.log("to slow you cut the ogers club, then he knocks you unconsis with a strong punch; then walks away"); }
else {
console.log("your speed and swords sharpness are engough you get away"); }
break;
case 'riddle':
var round = prompt('do you tell him what round as the moon but gold like the the sun(yes or no)').toLowerCase();
var what = prompt('do you ask what do you get when you cross a retorical question and a joke(yes or no)').toLowerCase();
if(round === 'yes' || what === 'no') {
console.log('The oger looks up at the sun, then says the sun, you say aa no a wedding ring, the oger says not my wedding ring, you get whomped');
}
else if (round === 'yes' && (what === 'yes')) {
console.log("The oger looks up at the sun, then says the sun, you then say what do you get when cross a retorical question with a joke the oger sits down to think and doesn't notice you tiptoeing away");
}
else {
console.log("The oger sits down to think and doesn't notice you walking away(the answer is a riddle)");
}
break;
case 'scare':
console.log("you shrunch up your face, raise your hands then make a hidious noise he is terified. If you had paid attention in school you would have know that ogers are to stupid to run away, you get whomped");
break;
default:
console.log("I don't understand but you would probably get whomped");

}