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]
Related
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!
I am currently new to JavaScript and am learning in school! There is an assignment that I am doing by creating a game but the alert keeps popping up with the wrong one. Every time it alerts as "You found a match!" for every card, however this is not supposed to happen. I have been trying to figure this out for the last hour. Thanks
var cards = ["queen", "king", "queen", "king"];
var cardsInPlay = [];
var cardOne = cards[0];
cardsInPlay.push(cardOne);
console.log("User flipped " + cardOne);
var cardTwo = cards[1];
cardsInPlay.push(cardTwo);
console.log("User flipped " + cardTwo);
if (cardsInPlay.length === 2){
cardsInPlay[0] === cardsInPlay[1];
alert("You found a match!");
} else {
alert("Sorry, try again");
}
You have a simple syntax error.
if (cardsInPlay.length === 2){
cardsInPlay[0] === cardsInPlay[1];
By putting your second conditional inside the bracket {, you've made it ineffective. Try this:
if (cardsInPly.length === 2 && cardsInPlay[0] === cardsInPlay[1]) {
The condition always goes inside the parenthesis ( ). If it's outside of it, it won't work.
Typing cardsInPlay[0] === cardsInPlay[1]; when they aren't equal is effectively like typing false;. It's technically valid, but doesn't do anything.
I think you ment to put the condition like below:
if (cardsInPlay.length === 2 && cardsInPlay[0] === cardsInPlay[1]) {
alert("You found a match!");
}
My first statement is working correctly but it shadows the else if statement below and just duplicates, maybe because the arguments are the same? I can't seem to figure out why since I am using semicolon and properly bracketing the conditions. I would love an explanation to the solution anyone comes up with!
function sayHiToGrandma(string) {
if (string.toLowerCase() === 'hello') {
return "I can't hear you!";
}
else if (string.toUpperCase() === 'HELLO') {
return "YES INDEED!";
}
}
In this case if your string is hello like any combinations HEllo or any combination with small h,e,l,l,o and H,E,L,L,O in your if statement you are getting lowercase hello so it is satisfied in the if so it will execute the if and ignores all the remaining conditions
Thats why only I cant hear you will be returned but it wont go to the else if condition
try this :http://www.w3schools.com/jsref/jsref_if.asp
if and else if with same condition only if will execute but not else if
NVM I found it out. Instead of actually using a 'string' i used the string argument like this,
function sayHiToGrandma(string) {
if (string === string.toLowerCase()) {
return "I can't hear you!";
}
else if (string === string.toUpperCase()) {
return "YES INDEED!";
} else if ('I love you, Grandma.') {
return 'I love you, too.'
}
etc.etc.etc.etc
thanks for the comments fellas
// 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");
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.");
}