Unexplained identifier


#1


https://www.codecademy.com/courses/javascript-beginner-en-ZA2rb/0/5?curriculum_id=506324b3a7dffd00020bf661#


I continue to get Unexpected identifier. Not sure what I'm missing.


ar user = prompt("What is your name?").toUpperCase();
switch(user){
    case 'Mary':
        console.log("Good to have you back");
        break;
     case 'Joe':
         console.log("Hi there");
         break;
    case 'Steve':
        console.log("Steve Holt?");
        break;
    default:
    console.log("I don't know");
}    

var user = prompt("You are in the jungle, suddenly a rhino charges! Do you Battle, Run or Stay still?").toLowerCase();

switch(user){
    case "battle":
        var start = prompt("Do you choose a shotgun or a rifle?").toLowerCase();
        var add = prompt("Do you want a 12 gauge or a scope?").toLowerCase();
        if(start ===" shotgun" || add === "12 gauge"){
        console.log("Might need something more!");
}
else if (start === "rifle" || add === "scope"){
    console.log("Good start!");
}
else if (start === "shotgun" || add === "scope"){
    console.log("Not sure that will help much");
}
else if(start === "rifle" || add === "12 gauge"){
    console.log("Really, might want to re-think that?");
}
else {
    console.log("You got squashed");
}    
break;
case "run":
    prompt("Are you fast? Yes or No?").toLowerCase();
    var headstart = prompt("Did you get a head start? Yes or No?").toLowerCase();
    var fast = prompt("Are you fast? Yes or No?").toLowerCase();
    if head start==="yes"&&fast==="yes"){
        console.log("You ran away successfully");
}
else if(head start==="yes" || fast==="yes"){
    console.log("At least you're one of those two, you got away");
}
else{
    console.log("You didn't get a head start and not fast you got squashed by the rhino");
}
break;
case "stay still":
    var scream = prompt("Can you scream? Yes or No?").toLowerCase();
    var loudness =prompt("Do you have a loud voice? Yes or No?").toLowerCase();
    if(scream ==="yes" && loudness === "yes"){
        console.log("Help is on the way!");
}
else if (scream==="yes" && loudness==="no"){
    console.log("Voice wasn't loud enough");
}
else {
    console.log("Good thing rhinos have bad eyesight");
}
break;
default:
console.log("You didn't enter a valid choice, try again");
}

#2

Hi atarockstar97710,

I found some syntax errors in your code. I have corrected it as below.

var user = prompt("What is your name?".toUpperCase());
switch(user){
    case 'Mary':
        console.log("Good to have you back");
        break;
     case 'Joe':
         console.log("Hi there");
         break;
    case 'Steve':
        console.log("Steve Holt?");
        break;
    default:
    console.log("I don't know");
}    

var user = prompt("You are in the jungle, suddenly a rhino charges! Do you Battle, Run or Stay still?".toLowerCase());

switch(user){
    case "battle":
        var start = prompt("Do you choose a shotgun or a rifle?".toLowerCase());
        var add = prompt("Do you want a 12 gauge or a scope?".toLowerCase());
        if(start ===" shotgun" || add === "12 gauge"){
        console.log("Might need something more!");
}
else if (start === "rifle" || add === "scope"){
    console.log("Good start!");
}
else if (start === "shotgun" || add === "scope"){
    console.log("Not sure that will help much");
}
else if(start === "rifle" || add === "12 gauge"){
    console.log("Really, might want to re-think that?");
}
else {
    console.log("You got squashed");
}    
break;
case "run":
    prompt("Are you fast? Yes or No?".toLowerCase());
    var headstart = prompt("Did you get a head start? Yes or No?".toLowerCase());
    var fast = prompt("Are you fast? Yes or No?".toLowerCase());
    if (headstart==="yes"&&fast==="yes"){
        console.log("You ran away successfully");
}
else if(headstart==="yes" || fast==="yes"){
    console.log("At least you're one of those two, you got away");
}
else{
    console.log("You didn't get a head start and not fast you got squashed by the rhino");
}
break;
case "stay still":
    var scream = prompt("Can you scream? Yes or No?".toLowerCase());
    var loudness =prompt("Do you have a loud voice? Yes or No?".toLowerCase());
    if(scream ==="yes" && loudness === "yes"){
        console.log("Help is on the way!");
}
else if (scream==="yes" && loudness==="no"){
    console.log("Voice wasn't loud enough");
}
else {
    console.log("Good thing rhinos have bad eyesight");
}
break;
default:
console.log("You didn't enter a valid choice, try again");
}

#3

Thank you. Is there a difference between?:

var start = prompt("Do you choose a shotgun or a rifle?".toLowerCase());

and

var start = prompt("Do you choose a shotgun or a rifle?").toLowerCase();


#4

Hi,

toLowerCase() and toUppercase() methods are used to convert a string to lowercase and uppercase respectively. So these methods will only work if you attch it to a string. In the above code "Do you choose a shotgun or a rifle?" is a string. That's why I have attached these methods to it.

 prompt("Do you choose a shotgun or a rifle?")

is not a string. So you cannot attach toLowerCase() or toUpperCase() to it.


#5

Hey there data,

I looked at your code and tried to dissect it myself.

I noticed a few things but not sure how to post the corrections (responding is new for me).

Firstly, I would change the variable that you used twice i.e. the "user" variable. Doing this can cause issues later down the line. In your first switch case also you have set your variable to Uppercase, therefore unless you change the cases i.e. "MARY", you'll keep getting the default case.

Secondly, on your case "battle" you'll want to change the if/elseif/else conditions to && (and not ||) so it has to make both conditions true and not only accept either one. You'll probably only have the 2nd condition met if you keep it as is.

The last two cases look fine to me.


#6

Hey again,

I also wanted to clarify this one also. They are not the same.

var start = prompt ("Do you choose a shotgun or a rifle?".toLowerCase())

will modify the question in the prompt to ask >>> do you choose a shotgun or a rifle? (notice how everything is lowercase?)

var start = prompt ("Do you choose a shotgun or a rifle?").toLowerCase()

is the correct usage if you want the answer to become lowercase i.e. shotgun and not Shotgun/SHOTGUN/SHOtguN.

The prompt is a string looking for an answer, attaching toLowerCase or toUpperCase modifies the answer received.


#7

Hi knarv,

Thank you for correcting me :slight_smile:
@datarockstar97710: knarv's answer is right. Sorry about my wrong answer.


#8

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