If statement issue with Var Prompt - javascript

I keep getting an error of "SyntaxError: Unexpected keyword 'else'". Can anyone help me figure as to why this is?
// Check if the user is ready to play!
confirm("I am ready to play!");
var age = prompt("What's your age?");
if(age === 13)
{
console.log("You are allowed to play but at your own risk.");
}
else
{
console.log("Play on!");
}
console.log("You are at a Justin Bieber concert, and you hear this
lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");
if(userAnswer === "yes")
{
console.log("You and Bieber start racing. It's neck and neck!
You win by a shoelace!");
}
else(userAnswer === "no")
{
console.log("Oh no! Bieber shakes his head and sings 'I set a pace,
so I can race without pacing.'");
}
var feedback = prompt("Rate Game 1-10");
if(feedback > 8)
{
console.log("Thank you! We should race at the next concert!");
else(feedback < 8)
{
console.log("I'll keep practicing coding and racing.");
I keep getting an error of "SyntaxError: Unexpected keyword 'else'". Can anyone help me figure as to why this is?

See this bit of code?
if(feedback > 8)
{
console.log("Thank you! We should race at the next concert!");
else(feedback < 8)
{
It should be:
if(feedback > 8)
{
console.log("Thank you! We should race at the next concert!");
}
else(feedback < 8)
{
You were missing a }. Also, note the comment from T.J. else doesn't take a condition, so really it should be:
if(feedback > 8)
{
console.log("Thank you! We should race at the next concert!");
}
else
{
The following is also problematic if this is how it exists in your code, but I'm going to assume it's a copy/paste issue. You can't split a string like this on to multiple lines unless you escape it.
console.log("You are at a Justin Bieber concert, and you hear this
lyric 'Lace my shoes off, start racing.'");

Some observations:
else doesn't take a condition, so lines like
else(userAnswer === "no")
don't do what you think they do, which leads to syntax errors later on. Instead:
else if (userAnswer === "no")
You can't just put a line break in the middle of a string literal like that, not with strings quoted with " or '. (You can with ES6's new template strings quoted with ```, but support is thin on the ground so far.)
Indentation is your friend. Use it. http://jsbeautifier.org or any half-decent IDE can help.
...and see Andy's answer about a missing }, which would be more obvious if you used indentation (#3 above).

There is closing } missing in your js
if(feedback > 8)
{
console.log("Thank you! We should race at the next concert!");
else(feedback < 8)
{
console.log("I'll keep practicing coding and racing.");
Corrected Code
if(feedback > 8)
{
console.log("Thank you! We should race at the next concert!");
}
else(feedback < 8)
{
console.log("I'll keep practicing coding and racing.");
}

Related

Stop a 'falsy' alert after a correct prompt in JavaScript

After answering the prompt correctly, the alert-message for wrong answer still appear after the right answer is given. I don't seem to find why nor how to terminate the second alert.
var userResponse = prompt("Hello, please enter the capital of Massachusetts?");
if(userResponse === "Boston") {
alert("Yes! Boston is the right answer");
};
if(userResponse === "Albany"){
alert("I am sorry, but Albany is rather the capital of NY");
}
else {
alert("I am sorry, but" + userResponse + " is not the right answer.");
};
Bad formatting / code style and a missing else:
var userResponse = prompt("Hello, please enter the capital of Massachusetts?");
if (userResponse === "Boston") {
alert("Yes! Boston is the right answer");
} else if (userResponse === "Albany") {
alert("I am sorry, but Albany is rather the capital of NY");
} else {
alert("I am sorry, but " + userResponse + " is not the right answer.");
}
Also note semicolons (;) are not necessary after code blocks ({}). In fact, in your case, one would have broken your if-else if chain.
Your code works! I think you must be simply typing it in wrong since it is case sensitive. I would add this so that it is not case sensitive:
if (userResponse === "Boston" || userResponse === "boston") {
Now you can type boston instead of only being able to type Boston. My recommendation is to never make the answer only case sensitive! In this case, it will say I am sorry, but boston is not the right answer., when it is totally correct
And make sure to add else on the second possibility as #AurelBily pointed out!

Syntax error resulting from invalid syntax in if / else statements

// Check if the user is ready to play!
confirm("Are you ready to play?");
var age = prompt("What's your age");
if ( age is less than 13)
{
console.log("You are allowed to play,but we take no responsibility");
}
else {
console.log("Go on! you can play");
}
I've got a syntax error while executing this JavaScript code, the first two lines (confirm and variable) are correct, this error is somewhere in the if / else satements.
Use < operator instead of is less than
// Check if the user is ready to play!
confirm("Are you ready to play?");
var age = prompt("What's your age");
if (age < 13) {
alert("You are allowed to play, but we take no responsibility");
} else {
alert("Go on! you can play");
}
You can reduce line of code also by using ternary operator
age < 13 ? console.log("You are allowed to play, but we take no responsibility"):console.log("Go on! you can play");

Why is only the first case of my switch list working correctly?

For the life of me, I can't figure out what's wrong with it. When I run this script, the first 'STAY' case works just fine. Running any of the other cases gives me an error (undefined is not a function).
Ran it thru my JS checker and it looked ok on there. Googled it and everyone else I'm seeing with the same problem has been told their breaks are either used incorrectly or not there. All of the syntax looks right to me from what I've learned. Compared it against the sample 'game' and it looks very similar to how they did it. What am I doing wrong?! Thank you for any help
var user = prompt("You see God. Do you want to STAY, PUNCH HIM, or CRY?").toUpperCase();
switch(user) {
case 'STAY':
var curious = prompt("Are you a curious person?").toUpperCase();
var insight = prompt("Are you an insightful person?").toUpperCase();
if (curious === "YES" && insight === "YES") {
console.log("Maybe it was a good idea to stay and speak to him");
} else if (curious === "YES" || insight === "YES") {
console.log("Well, maybe you can scrunge up something to say");
} else {
console.log("Why would you stay if you have nothing intelligent to say?");
}
break;
case 'PUNCH HIM':
var strong = prompt("Are you ridiculously stronger than God?").toUpperCase();
var fast = prompt("Are you faster than a minute man?").toUpperCase();
if (strong === "YES" && fast === "YES") {
console.prompt("You still dead, but not as dead as you would've been");
} else if (strong === "YES" || fast === "YES") {
console.prompt("One ain't good enough, homie");
} else {
console.prompt("Slow and weak? Bad choice, dag");
}
break;
case 'CRY':
var convincing = prompt("Are you superbly convincing with crying?").toUpperCase();
var female = prompt("Are you a female?").toUpperCase();
if (convincing === "YES" && female === "YES") {
console.prompt("You'll prolly be ok, boo");
} else if (convincing === "YES" || female === "YES") {
console.prompt("Hope you're a female");
} else {
console.prompt("You dead!");
}
break;
default:
console.prompt("Answer the question with the supplied answers");
}
Use console.log instead of console.prompt.

Syntax Error: Unexpected token Else

I am learning Javascript via Codecademy and no have been stumped on this little piece here.
I have supposed to write an if else statement.
It shows me here in the following that there is a Syntac Error with a missing identifier:
var userAnswer = prompt("Are you feeling lucky, punk?");
if (userAnswer === "yes");
{
console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}
else {
console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}
What is wrong with that.... There is no error in this example here:
if (age < 18)
{
console.log("We take no actions or responsibility. Play at your own risk!");
}
else
{
console.log("Enjoy the game");
}
if (userAnswer === "yes");
Remove the semicolon.
There's a semi-colon after the first conditional check. Also, you should always put the opening bracket of the conditional branch on the same line as the brackets
var age;
age = prompt('How old are you?');
if (age < 18)
{
alert("We take no actions or responsibility. Play at your own risk!");
}
else if(age > 18)
{
alert("Enjoy the game");
}
remove the semicolon after
if (userAnswer === "yes");
if you put the semicolon there, you are telling the script to stop there and not to render the next conditional statement that is "else"[SyntaxError: Unexpected token else]

case user.length < 4; console.log("nametoo short :("); break; Why doesn't this work?

I'm working on switch statements and I've been trying to make this code right here work, but it doesn't seem to output the correct console.log case string.
var user = prompt("What is your name?").toLowerCase();
switch(user){
case "luka":
console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
break;
case user.length > 10:
console.log("That's a long name!");
break;
case user.length < 4:
console.log("Not to be rude or impolite to you in any way, but your name is kinda short :( Not that it isn't cool or something :D");
break;
}
I've tried putting paranthesis around the user like this (user).length < 4, but that doesn't work, nor some of my other tries. Does anybody know how to implement this correctly?
You should not use conditionals in a switch statement.
Use if/else if
var user = prompt("What is your name?").toLowerCase();
if (user==="luka") {
console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
} else if (user.length > 10) {
console.log("That's a long name!");
} else if (user.length < 4) {
console.log("Not to be rude or impolite to you in any way, but your name is kinda short :( Not that it isn't cool or something :D");
} else {
console.log("in else");
}
There is one possible workaround for using switch in the cases like yours:
var user = prompt("What is your name?").toLowerCase();
switch (true) {
case (user === "luka"):
console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
break;
case (user.length > 10):
console.log("That's a long name!");
break;
case (user.length < 4):
console.log("Not to be rude or impolite to you in any way, but your name is kinda short :( Not that it isn't cool or something :D");
}
However I'd follow #epascarello's advice and use if/else blocks.
That's just not how JavaScript switch statements work. The values in the "case" expressions are compared to the value of the switch expression.
The statement you have there is equivalent to:
if (user === "luka") {
console.log("What a beautiful name, my name is exactly the same :) Hi Luka! :D");
}
else if (user === (user.length > 10)) {
console.log("That's a long name!");
}
else if (user === (user.length < 4)) {
console.log("Not to be rude or impolite to you in any way, but your name is kinda short :( Not that it isn't cool or something :D");
}
Thus, you're comparing the value of "user" to the results of comparing user.length to those values. Those comparison results are boolean, so "use" will never be === to them.

Categories

Resources