Author Topic: Codecademy's Javascript  (Read 2829 times)

So, I figured this doesn't go under Coding Help, because it says, "Torque Script problem?" when this is JS.

Basically, I'm trying to get through one of the exercises by making a Choose Your Own Adventure thing (anyone who has gone through Codecademy probably knows what I'm talking about), but it is giving me errors on my code so I figure I'm using a syntax problem.

Code: [Select]
// Check if the user is ready to play!
confirm("Are you ready to play?");
var age = prompt("How old are you?");
if (age < 6)
{
    console.log("You have stated that you are under the age of 6. You are allowed to play, but I do not take responsibility for anything that may happen.");
}
else
{
    console.log("Welcome to Choose Your Own Adventure!");
}

Then when I run the code, an error message comes up:

Quote from: Script.js
Oops, try again. It looks like you didn't check for the user's age.

But I already did this.  Can someone get a run-through on this?

Do you have to convert the age to a number? Try using "age = parseInt(age)" to convert it to a number first.

Do you have to convert the age to a number? Try using "age = parseInt(age)" to convert it to a number first.

It has never used that cmd yet, so I doubt it.  If helpful, I'll post the instructions too.

Quote
Old enough to play?
With great programming powers, comes great responsibility. We should check if the user is old enough to play!

This involves two bits of code.

Using prompt to ask the user for their age. You can do it like this: var age = prompt("What's your age?"); and the variable age will hold the user's response.

Writing an if / else statement based on the age of the user.

Note: we have to store the result of the prompt in a variable! Otherwise, we can't make use of it in the if / else statement.

Instructions
Under the existing code, declare a variable age.
Make age equal to the answer you prompt the user to give (ie. ask the user 'how old are you?')
If the user is less than 18, use console.log and tell them they're allowed to play but you take no responsibility.
Else, use console.log and give them an encouraging message to play on!

Looks ugly, thanks to copy and paste.  Sorry.

You have to check if they are younger than 18, not 6.

You have to check if they are younger than 18, not 6.

I tried that first and when it didn't work, I just decided to personalize it.

I have a new idea though.  I'll see if it works.

Edit: Weird...

I copied and pasted the code, cleared it, and re-did it, putting the age back to 18 and it worked.  I guess I was just being stupid lol
« Last Edit: January 05, 2014, 12:32:13 AM by XR-7 »

I went through Codeacademy JavaScript series, I remember having trouble with this one. Glad to hear you figured it out.

I went through Codeacademy JavaScript series, I remember having trouble with this one. Glad to hear you figured it out.

Yeah I got the rest of that lesson finished.  Moving on.