/    Sign up×
Community /Pin to ProfileBookmark

Nested if-statement help – please!

Hi,

I’m trying to nest an if-statement. I want to quiz the user with the question “What is the capital of Kenya?”. If they get it right, a message congratulating them will be displayed. If they get it wrong, I want to give them one more change. If they still get it wrong, I want to display a message that says “better luck next time”.

Here’s my code so far:

var question = “What is the capital of Kenya?”;
var answer = Nairobi;

var guess = window.prompt(question, “Enter your answer here”);

if (guess == answer) {
window.alert(“CONGRATULATIONS!!!”);
if (guess != answer)
guess = window.prompt(question, “Go on, have another go!”);
window.alert(“Second time lucky! Well done!);
}
else {
document.writeln(“I’m sorry, better luck next time”);
window.alert(“Unlucky mate!”);
}

I can’t get it to work… PLEASE HELP!!

What am i doing wrong?

Thanks ?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@fredmvJan 31.2004 — [i]Originally posted by london_city_guy [/i]

[B]var answer = [color=red]'[/color]Nairobi[color=red]'[/color]; [/B][/QUOTE]
Copy linkTweet thisAlerts:
@David_HarrisonJan 31.2004 — Not the only problem fred. ?

You did not put quotes around the answer and the logic of your if/else statements needed changing a bit, other than that it was almost there. Just as a good habit to pick up, you might also want to always use {} for your if's and else's.

<i>
</i>
var question = "What is the capital of Kenya?";
var answer = "Nairobi";

var guess = prompt(question, "Enter your answer here");

if (guess == answer){alert("CONGRATULATIONS!!!");} // End of outside if

else{

guess = prompt(question, "Go on, have another go!");

if(guess == answer){alert("Second time lucky! Well done!");} // End of inside if
else{
alert("Unlucky mate!");
document.writeln("I'm sorry, better luck next time");
} // End of inside else

} // End of outside else

Once you copy and paste this into notepad, the comments should line up and be easier to read.

Edit: I found that you also missed of a closing quote in one of your alerts.
Copy linkTweet thisAlerts:
@fredmvJan 31.2004 — Good catch. I didn't really look over the code too much, as you can tell. ?
Copy linkTweet thisAlerts:
@london_city_guyauthorJan 31.2004 — Wow - you guys are great!!

Thanks very much.

I'm a novice at this as you can tell.. slowly but surely, i'll get there in the end.. i hope.

I need to do a few more things:

  • - nest a loop within an if-statement


  • and

  • - nest an if-statement within a loop


  • any idea's of how i can start these?

    cheers ?
    Copy linkTweet thisAlerts:
    @buntineJan 31.2004 — Here ya go..

    Loop within an if-statement.
    [code=php]
    if (expression) {
    for (var i=0;i<10;i++) {
    alert("Number: "+i);
    }
    }
    [/code]


    If-statement within a loop:
    [code=php]
    for (var i=0;i<10;i++) {
    if (i <= 5) {
    alert("Variable is 5 or less.");
    } else {
    alert("Variable is greater than 5");
    }
    }
    [/code]


    Regards,

    Andrew Buntine.
    ×

    Success!

    Help @london_city_guy spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...