4

I am follow the course of CodeAcademy for JS, but there's a problem, the script work but I

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 {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

Give me error:

Oops, try again. Did you add an if statement to your code?

Here page: https://www.codecademy.com/courses/javascript-beginner-en-x9DnD/0/5?curriculum_id=506324b3a7dffd00020bf661, it's in Code Your Own Adventure! 5.

1
  • 3
    well, your code works (the expected output is printed). I'd report this as a bug to codeacademy. Commented Apr 7, 2016 at 13:03

5 Answers 5

2

I am guessing it could be a bug in Codeacadamey. Strangely, the code below works and gives me a green icon.

Writing an if, else if and else statement can sort out these questions. It may be because the instructions are not very clear.

// Check if the user is ready to play!
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 if (userAnswer === 'no') {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
} else {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your code works for me in a browser and I can see the result in the browser console window, so I think you should report this as a bug.

However, you are only checking for lower-case yes. Yes or YES will not match the if statement and will run the code in the else block.

You could try:

if(userAnswer.toLowerCase() === "yes") {    
    // Your code here
} else {
    // Your code here
}

2 Comments

That's why I said you should report it to CodeAcademy. I was pointing out that entering Yes wouldn't work as you are only checking for lower-case answers.
As @thalaivar says, it works if you add a check for no. I'd contact CodeAcademy and report this. Let us know what they say.
0

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.toLowerCase() == "yes") {
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}
else {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

2 Comments

normally its work, but try on given link in question
the code posted by op is also correct.question is why codeaccdemy give the error "Oops, try again. Did you add an if statement to your code?" .if you post your codes to that page it gives you same error
0

The content should be as below, it might possible that you have deleted something.

// 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("They're allowed to play but you take no responsibility");
} else {
    console.log("Play On");
}
// Check if the user is ready to play!
var introduction  = "You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'";

console.log(introduction);
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 {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

Comments

0

Occasionally, Codeacademy incorrectly reports errors in your code based on trivial whitespace differences. Based on the output of your script, you've written your code correctly.

Funnily enough, using a ternary operator instead of an if statement works fine:

// Check if the user is ready to play!

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?");

console.log( userAnswer === "yes" ?
    "You and Bieber start racing. It's neck and neck! You win by a shoelace!" :
    "Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'"
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.