Please explain what I did wrong (exercise four and five)?


#1

Exercise Four: "Switch"

var user = prompt ("Do you want to play a game? Yes? No?").toUpperCase();

switch(user) {
case 'Character':
console.log("You are a lonely travler on a quest to restore status to your family and wealth to your person. Cliche? Yes, but this will not be entire narrative.");
break;
case 'Environment':
console.log("You reside in a world that is both physical and supernatural. In addition, in this society, the Earth has been devestated by an international war and the remnants of humanity can space travel. A universe that would make any otaku stand in awe!");
break;
case 'Items':
console.log("In this world, technology that spans the last ten centuries reside. Swords, Bows, Guns, Lasers, Steam Engines, Combustion Engines, Wheeled-Vehicles, Hover-Vehicles, the whole nine-yards!");
break;
default:
console.log("You" + user + "will undergo an adventure that will be unforgetable.");
}

This exercise was correct. The problem lies in exercise number five.

Exercise Five: Logical Operators

var user = prompt ("Do you want to play a game? Yes? No?").toUpperCase();

switch(user) {
case 'Character':
var Selection = prompt ("Do you want to take a quest (YES or NO)?").toUpperCase();
var Chosen = prompt ("Do you want to be a lonely traveller?").toUpperCase();
if (Selection === 'Yes' || Chosen === 'Yes')
return ("Congratulations Warrior, the path you have chosen will be rigorous, but rewarding!");
} else {
return ("No? The destiny most sought is found on the path least traveled and when one is alone. You lose.");
}
break;
case 'Environment':
var Routine = prompt ("Do you come from a distant village in the country(YES or NO)?").toUpperCase();
var Change = prompt ("Do you want to leave now?").toUpperCase();
if(Routine === 'Yes' && Change === 'Yes'){
return ("Wonderful! In your desire to make haste, the elder of the village gave you a map of the surrounding area. You can now avoid robbers, ferocious wild animals, and landscapes to dangerous to navigate.");
} else {
return ("Oh no! A group of raiders, who descended from the mountains, have attacked your village. In the midst of the chaos, you lost your life.");
}
break;
case 'Items':
var Treasure = prompt ("Do you want to use items to assist you in your journey(YES or NO)?").toUpperCase();
var Heirloom = prompt ("Do you want them now?").toUpperCase();
if(Treasure === 'Yes' || Heirloom === 'Yes'){
return ("YOU ARE UNSTOPPABLE! No human, alein, or demon can get in your way.");
} else {
return ("I knew you well. I hope that you will find comfort in the afterlife.");
}
break;
default:
return ("Your name will be greater than all warriors of the past, combined!");
}

I think that the problem lies in the fact that I deviated from the previous syntax. Overall, I struggled with making re-writing exercise four in order to execute the "if/else" statements.


#2

If user types yes the method String.toUpperCase gives you YES.

So check your code and correct it on all switch cases and if statements


#3

Do you mean add the option "No"?


#4

var Selection = prompt ("Do you want to take a quest (YES or NO)?").toUpperCase();

I type now "yes" toUpperCase makes my String to "YES"

var Selection has now the value = "YES"

Here you check is Selection Yes it could never be Yes cause you use .toUpperCase

if (Selection === 'Yes' || Chosen === 'Yes')

#6

So change ".toUpperCase();" to ".toLowerCase();" and that should make the code function properly?


#7

Nope,

you doesnt compare the rigth values

'Yes' is not 'YES or 'yes'

Change this Selection === 'Yes' to Selection === 'YES'


#9

I performed the recommendation that you have given me and I still receive an error message. I must have done something wrong elsewhere in the code.


#10

Please post the new code here.
And the Error message


#11

var user = prompt ("Do you want to play a game? Yes? No?").toUpperCase();

switch(user) {
case 'Character':
var Selection = prompt ("Do you want to take a quest (YES or NO)?").toUpperCase();
var Chosen = prompt ("Do you want to be a lonely traveller?").toUpperCase();
if(Selection === 'YES' || Chosen === 'YES')
return("Congratulations Warrior, the path you have chosen will be rigorous, but rewarding!");
} else {
return("No? The destiny most sought is found on the path least traveled and when one is alone. You lose.");
}
break;
case 'Environment':
var Routine = prompt ("Do you come from a distant village in the country(YES or NO)?").toUpperCase();
var Change = prompt ("Do you want to leave now?".toUpperCase();
if(Routine === 'YES' && Change === 'YES'){
return("Wonderful! In your desire to make haste, the elder of the village gave you a map of the surrounding area. You can now avoid robbers, ferocious wild animals, and landscapes to dangerous to navigate.");
} else {
return("Oh no! A group of raiders, who descended from the mountains, have attacked your village. In the midst of the chaos, you lost your life.");
}
break;
case 'Items':
var Treasure = prompt ("Do you want to use items to assist you in your journey(YES or NO)?").toUpperCase();
var Heirloom = prompt ("Do you want them now?").toUpperCase();
if(Treasure === 'YES' || Heirloom === 'YES'){
return("YOU ARE UNSTOPPABLE! No human, alein, or demon can get in your way.");
} else {
return("I knew you well. I hope that you will find comfort in the afterlife.");
}
break;
default:
return("Your name will be greater than all warriors of the past, combined!");
}

The error message is this: "SyntaxError: Illegal return statement."

I have used the "console.log" and that too did not work.


#12

Like i explain

var user = prompt ("Do you want to play a game? Yes? No?").toUpperCase();

switch(user) { 
case 'Character': // all this cases will never work think about Method .toUpperCase

#13

Transform the "Yes" into "YES" and the "No" into "NO". Do the additional parentheses in the string pose as a problem for the syntax?


#14

Character is not CHARACTER.

So you should change the all Cases in your switch too