Syntaxerror: Syntaxfehler I can not find it


#1


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


It says Syntaxerror, but i can not find it. Can you help me?


var user = prompt("You are riding on a horse through wilderness. You meet a guy with black hair. He asks you something but you can't understand. He looks a bit creepy. Do you RIDE away, jump off your horse and ASK him again what he said or will you FIGHT him?").toUpperCase();

switch (user) {
    
    case 'RIDE':
        console.log("You did it and ride away but you will never find out what his guy said. You come to a point where your road crosses");
        var weg = prompt("Will you go the LEFT or RIGHT way?").toUpperCase();
        
        if (weg === 'RIGHT') {
            console.log("You chose the right way! You find the way home and arrive there safely.");
        }
        
        else if (weg === 'LEFT'){
            console.log("You made the wring choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
        }
        
        else{
            prompt("You have to choose LEFT or RIGHT!");
           
             if(weg === 'LEFT'){
            console.log("You made the wring choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
            }
             else if (weg === 'RIGHT') {
            console.log("You chose the right way! You find the way home and arrive there safely.");
          }
        }
             else{
              console.log("You did not get it! You loose!");
          }
        }
        break;
    
    
    case 'ASK':
    console.log("He looks up and repeats what he said before. He told you about his daughter laying on the ground with injuries a few hundred meters away: The way you have to surmount is dangerous to ride with your horse. Do you want to DO it anyway oder will you GET OFF your horse and try to rescue her by foot?")
    var horse = prompt("Do you want get there by horse? Yes or No?").toUpperCase();
    if(horse === 'YES') {
        console.log("Your horse souvereignly mastered it! You made it. You reach a point where you hear a woman cry. But it is almost night. You have to choose if you wait until it is morning or if you set out to rescue her.");
        var rescue = prompt("Do you want to set out NOW or in the MORNING?").toUpperCase();
   
        
        if(rescue==='NOW'){
            console.log("You found her laying on the ground even if you did not see that much! You rescued her. In the end you find out her father is the king of the place where you live. Therefore she is a princess - you get a big bag of gold and it is enough to live with until you die. You are a great hero!");
        }
        else if(rescue==='MORNING') {
            console.log("You start your quest in the morning. You looked for her a couple of hours. At least you found her but she is dead. You loose!");
        }
        else{
            prompt("You can only choose NOW or MORNING. Choose again!).toUpperCase();
            if(rescue==='NOW'){
            console.log("You found her laying on the ground even if you did not see that much! You rescued her. In the end you find out her father is the king of the place where you live. Therefore she is a princess - you get a big bag of gold and it is enough to live with until you die. You are a great hero!");
            }
            else if (rescue==='MORNING') {
            console.log("You start your quest in the morning. You looked for her a couple of hours. At least you found her but she is dead. You loose!");
        }
        else{
            console.log("You are very silly! LOOOSER");
        }
        }
        else{
            console.log("You were killed by a lion, because you run in the wrong direction. You loose");
        }
        break;
    
    
    case 'FIGHT':
        console.log("He kills you in the shortest amount of time. You loose!");
        break;
    default:
    console.log("You have to choose between RIDE, ASK and FIGHT!");
    break;
}


#2

Make sure you close your strings properly. I see one without a closing quotation mark

else{
     prompt("You can only choose NOW or MORNING. Choose again!).toUpperCase();
     if(rescue==='NOW'){

You also have an extra else statement. You can't have 2 else statements like this:
else statements are used when all other conditions have been exhausted.

else { 
    //do something
} else {
    //do something
};

This tells me that you didn't exhaust all the other conditions in the first else.
I see it in your ASK case.

In your ASK case, you are also missing a closing curly bracket } before your break; It is because you have an if statement that wraps the whole thing.

Make sure your prompts are stored. Eg You have just

 prompt("You have to choose LEFT or RIGHT!");

in your RIDE case but it should really be

var weg =  prompt("You have to choose LEFT or RIGHT!");

#3

The syntax highlighting should be a pretty big hint about unclosed strings
You can also run your code in a different interpreter, most will report the line numbers of encountered problems.


#4

First:

 else{
            prompt("You have to choose LEFT or RIGHT!");
           
             if(weg === 'LEFT'){
            console.log("You made the wring choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
            }
             else if (weg === 'RIGHT') {
            console.log("You chose the right way! You find the way home and arrive there safely.");
          }
        } // remove this }

becuse your next else should be inside of first else

second:

else{
prompt("You can only choose NOW or MORNING. Choose again!)

you forgot to close " " .need a " after Choose again!

Third:

else{
            console.log("You are very silly! LOOOSER");
        }
        }
        else{
            console.log("You were killed by a lion, because you run in the wrong direction. You loose");
        }

if you really want to print two statement then you dont need to create double else.Do it like

else{
   console.log("You are very silly! LOOOSER");
  console.log("You were killed by a lion, because you run in the wrong direction. You loose");
} // closing current else
}  //closing first else
}  //closing if

it'll run now.but do what @datfatcat said

Make sure your prompts are stored. Eg You have just

prompt("You have to choose LEFT or RIGHT!");

in your RIDE case but it should really be

var weg = prompt("You have to choose LEFT or RIGHT!");

and do same for rescue too

prompt("You can only choose NOW or MORNING. Choose again!").toUpperCase();


#5

Thank you for your help you all! I corrected it as best as I can, but now there is a new error:

SyntaxError: 'break' außerhalb einer Schleife nicht möglich

I think it is an issue with a curly bracket?

here is my new code..

var user = prompt("You are riding on a horse through wilderness. You meet a guy with black hair. He asks you something but you can't understand. He looks a bit creepy. Do you RIDE away, jump off your horse and ASK him again what he said or will you FIGHT him?").toUpperCase();

switch (user) {

case 'RIDE':
    console.log("You did it and ride away but you will never find out what his guy said. You come to a point where your road crosses");
    var weg = prompt("Will you go the LEFT or RIGHT way?").toUpperCase();

    if (weg === 'RIGHT') {
        console.log("You chose the right way! You find the way home and arrive there safely.");
    }

    else if (weg === 'LEFT'){
        console.log("You made the wring choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
    }

    else{
    var weg = prompt("You have to choose LEFT or RIGHT!");

         if(weg === 'LEFT'){
        console.log("You made the wring choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
        }
         else if (weg === 'RIGHT') {
        console.log("You chose the right way! You find the way home and arrive there safely.");
      }

         else{
          console.log("You did not get it! You loose!");
      }
    }
    break;


case 'ASK':
console.log("He looks up and repeats what he said before. He told you about his daughter laying on the ground with injuries a few hundred meters away: The way you have to surmount is dangerous to ride with your horse. Do you want to DO it anyway oder will you GET OFF your horse and try to rescue her by foot?")
var horse = prompt("Do you want get there by horse? Yes or No?").toUpperCase();
if(horse === 'YES') {
    console.log("Your horse souvereignly mastered it! You made it. You reach a point where you hear a woman cry. But it is almost night. You have to choose if you wait until it is morning or if you set out to rescue her.");
    var rescue = prompt("Do you want to set out NOW or in the MORNING?").toUpperCase();


    if(rescue==='NOW'){
        console.log("You found her laying on the ground even if you did not see that much! You rescued her. In the end you find out her father is the king of the place where you live. Therefore she is a princess - you get a big bag of gold and it is enough to live with until you die. You are a great hero!");
    }
    else if(rescue==='MORNING') {
        console.log("You start your quest in the morning. You looked for her a couple of hours. At least you found her but she is dead. You loose!");
    }
    else{
  var rescue = prompt("You can only choose NOW or MORNING. Choose again!").toUpperCase();
        if(rescue==='NOW'){
        console.log("You found her laying on the ground even if you did not see that much! You rescued her. In the end you find out her father is the king of the place where you live. Therefore she is a princess - you get a big bag of gold and it is enough to live with until you die. You are a great hero!");
        }
        else if (rescue==='MORNING') {
        console.log("You start your quest in the morning. You looked for her a couple of hours. At least you found her but she is dead. You loose!");
    }
    else{
        console.log("You are very silly! LOOOSER");
        console.log("You were killed by a lion, because you run in the wrong direction. You loose");
    }
    }


    }
}
    break;


case 'FIGHT':
    console.log("He kills you in the shortest amount of time. You loose!");
    break;
default:
console.log("You have to choose between RIDE, ASK and FIGHT!");
break;

}


#6

else{
        console.log("You are very silly! LOOOSER");
        console.log("You were killed by a lion, because you run in the wrong direction. You loose");
    }
    }


    }
}   //remove this }
    break;

and

default:
console.log("You have to choose between RIDE, ASK and FIGHT!");
break;  //remove break;

default doesn't need break
and close ( }) the switch statement.

default:
console.log("You have to choose between RIDE, ASK and FIGHT!");
}

#7

thank you for your help. But i now have to bring in the Logical Operators, too. Maybe you can help me again if i can not do it right.


#8

Hello again, i tried this time to integrate the || Operator.. i looked one hour for the new syntaxmistake, but i can not find it.
The error reads: SyntaxError: Syntaxfehler, Oops, try again. There was a problem with your syntax.

here is my new code..

var user = prompt("You are riding on a horse through wilderness. You meet a guy with black hair. He asks you something but you can't understand. He looks a bit creepy. Do you RIDE away, jump off your horse and ASK him again what he said or will you FIGHT him?").toUpperCase();

switch (user) {

case 'RIDE':
    console.log("You did it and ride away but you will never find out what his guy said. You come to a point where your road crosses");
    var weg = prompt("Will you go the LEFT or RIGHT way?").toUpperCase();

    if (weg === 'RIGHT') {
        console.log("You chose the right way! You find the best way to get home but you meet an old friend..");
 var tall = prompt("Are you tall? (YES or NO)?").toUpperCase();
var smart = prompt("Are you smart?").toUpperCase();

if(tall === 'YES' || smart === 'YES') {
  console.log("You only need one of the two! Nice work!");   
}
else{
 console.log("You have to be one of the two! Soooo you loose..");   
}
    }

    else if (weg === 'LEFT'){
        console.log("You made the wring choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
    }

    else{
    var weg = prompt("You have to choose LEFT or RIGHT!");

         if(weg === 'LEFT'){
        console.log("You made the wrong choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
        }
         else if (weg === 'RIGHT') {
        console.log("You chose the right way! You find the best way to get home but you meet an old friend..");
 var tall = prompt("Are you tall? (YES or NO)?").toUpperCase();
var smart = prompt("Are you smart?").toUpperCase();

if(tall === 'YES' || smart === 'YES') {
  console.log("You only need one of the two! Nice work!");  
}
else{
 console.log("You have to be one of the two! Soooo you loose..");   
}

    }
      }

         else{
          console.log("You did not get it! You loose!");
      }

    
    break;


case 'ASK':
console.log("He looks up and repeats what he said before. He told you about his daughter laying on the ground with injuries a few hundred meters away: The way you have to surmount is dangerous to ride with your horse. Do you want to DO it anyway oder will you GET OFF your horse and try to rescue her by foot?");
var horse = prompt("Do you want get there by horse? Yes or No?").toUpperCase();
if(horse === 'YES') {
    console.log("Your horse souvereignly mastered it! You made it. You reach a point where you hear a woman cry. But it is almost night. You have to choose if you wait until it is morning or if you set out to rescue her.");
    var rescue = prompt("Do you want to set out NOW or in the MORNING?").toUpperCase();


    if(rescue==='NOW'){
        console.log("You found her laying on the ground even if you did not see that much! You rescued her. In the end you find out her father is the king of the place where you live. Therefore she is a princess - you get a big bag of gold and it is enough to live with until you die. You are a great hero!");
    }
    else if(rescue==='MORNING') {
        console.log("You start your quest in the morning. You looked for her a couple of hours. At least you found her but she is dead. You loose!");
    }
    else{
  var rescue = prompt("You can only choose NOW or MORNING. Choose again!").toUpperCase();
        if(rescue==='NOW'){
        console.log("You found her laying on the ground even if you did not see that much! You rescued her. In the end you find out her father is the king of the place where you live. Therefore she is a princess - you get a big bag of gold and it is enough to live with until you die. You are a great hero!");
        }
        else if (rescue==='MORNING') {
        console.log("You start your quest in the morning. You looked for her a couple of hours. At least you found her but she is dead. You loose!");
		}

    }
}
    else{
        console.log("You are very silly! LOOOSER");
        console.log("You were killed by a lion, because you run in the wrong direction. You loose");
    }

break;



case 'FIGHT':
    console.log("He kills you in the shortest amount of time. You loose!");
break;


default:
console.log("You have to choose between RIDE, ASK and FIGHT!");

}

Thank you for your help again !


#9

telling you answer its not good because you should learn from you mistake.
in your code problem is just opening({) and closing (})braket.
you should use it properly.

 else{
    var weg = prompt("You have to choose LEFT or RIGHT!");

         if(weg === 'LEFT'){ //opening if
        console.log("You made the wrong choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
        } // closing if
        else if (weg === 'RIGHT') { //opening else if
        console.log("You chose the right way! You find the best way to get home but you meet an old friend..");
        var tall = prompt("Are you tall? (YES or NO)?").toUpperCase();
        var smart = prompt("Are you smart?").toUpperCase();

            if(tall === 'YES' || smart === 'YES') { //opening if
              console.log("You only need one of the two! Nice work!");  
            } //closing if
         else{  //oprning else
         console.log("You have to be one of the two! Soooo you loose..");   
            }  //closing else

             } // closing else if
          } // problem is here.read below 1
         else{
          console.log("You did not get it! You loose!");
      }
 //  # should be close it here.

problem:
if you closed there(1 ) it means you are closing else statement.so you have two else statement now.should it be?
because we use else statement when all upper condition doesn't match.so you can't have two else statement.close it where # sign is.

and you can do what ionatan said

You can also run your code in a different interpreter, most will report the line numbers of encountered problems.

hope you've understand now :slight_smile:


#10

I changed the whole Thing now, because it is too complicated to add this to my existing code.
I did it and it works perfectly fine!

Thank you all for your detailled help!
It helped me a lot!

Here is my code! For the ones who want to see how it works now, or for the ones who want to take this as a template!

Here is my Code:

var user = prompt("You are riding on a horse through wilderness. You meet a guy with black hair. He asks you something but you can't understand. He looks a bit creepy. Do you RIDE away, jump off your horse and ASK him again what he said or will you FIGHT him?").toUpperCase();

switch (user) {

case 'RIDE':
    console.log("You did it and ride away but you will never find out what his guy said. You come to a point where your road crosses");
    var weg = prompt("Will you go the LEFT, RIGHT or STRAIGHT way?").toUpperCase();

    if (weg === 'RIGHT'||weg==='STRAIGHT') {
        console.log("You chose the right way! The right one and the straight one both lead you to your home. You arrive there unjured.");
    }

    else if (weg === 'LEFT'){
        console.log("You made the wrong choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
    }

    else{
    var weg = prompt("You have to choose LEFT, RIGHT or STRAIGHT!");

         if(weg === 'LEFT'){
        console.log("You made the wrong choice! You came to a cliff and you and your horse fell down. Both of you are dead! You loose..");
        }
         else if (weg === 'RIGHT'||weg === 'STRAIGHT') {
        console.log("You chose the right way! The right one and the straight one both lead you to your home. You arrive there unjured.");
         }

         else{
          console.log("You did not get it! You loose!");
      }
         }

    break;


case 'ASK':
console.log("He looks up and repeats what he said before. He told you about his daughter laying on the ground with injuries a few hundred meters away: The way you have to surmount is dangerous to ride with your horse.");
var horse = prompt("Do you want get there by horse? Yes or No?").toUpperCase();
var finish = prompt("You think your horse can do this?").toUpperCase();
if(horse === 'YES'&&finish==='YES') {
    console.log("Your horse souvereignly mastered it! You made it. You reach a point where you hear a woman cry. But it is almost night.");
    console.log("You have to choose if you wait until it is morning or if you set out to rescue her.");
}
else {
    console.log("You have to believe in that you can do it..This attitude is never good.. you loose!");
}
 
 
 
var rescue = prompt("Do you want to set out NOW or in the MORNING?").toUpperCase();
    if(rescue==='NOW'){
        console.log("You found her laying on the ground even if you did not see that much! You rescued her. In the end you find out her father is the king of the place where you live. Therefore she is a princess - you get a big bag of gold and it is enough to live with until you die. You are a great hero!");
    }
    else if(rescue==='MORNING') {
        console.log("You start your quest in the morning. You looked for her a couple of hours. At least you found her but she is dead. You loose!");
    }
    else{
  var rescue = prompt("You can only choose NOW or MORNING. Choose again!").toUpperCase();
        if(rescue==='NOW'){
        console.log("You found her laying on the ground even if you did not see that much! You rescued her. In the end you find out her father is the king of the place where you live. Therefore she is a princess - you get a big bag of gold and it is enough to live with until you die. You are a great hero!");
        }
        else if (rescue==='MORNING') {
        console.log("You start your quest in the morning. You looked for her a couple of hours. At least you found her but she is dead. You loose!");
		}

    else{
        console.log("You are very silly! LOOOSER");
        console.log("You were killed by a lion, because you run in the wrong direction. You loose");
    }
    }
break;



case 'FIGHT':
    console.log("He kills you in the shortest amount of time. You loose!");
break;
    
    
default:
console.log("You have to choose between RIDE, ASK and FIGHT!");

}

#11

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