Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

Jose Esplana
Jose Esplana
12,503 Points

ELSE doesn't function when adding document.write for every ELSE IF

Hi! Can someone please explain Why ELSE doesn't function when I add document.write for every ELSE IF THANK YOU!

var correctGuess = false; var randomNumber = Math.floor(Math.random() * 6 ) + 1; alert(randomNumber); var guess = prompt('I am thinking of a number between 1 and 6. What is it?');

if (parseInt(guess) === randomNumber ) { correctGuess = true; document.write("<h1><b>AWESOME JOB!</b></h1>");

} else if (parseInt(guess) < randomNumber){ var guessMore = prompt('Please try again! Go higher!'); if (parseInt(guessMore) === randomNumber){ correctGuess = true; document.write("<h1><b>GREAT JOB!</b></h1>"); }
} else if (parseInt(guess) > randomNumber){ var guessLess = prompt('Please try again! Go lower!');
if (parseInt(guessLess) === randomNumber){ correctGuess = true; document.write("<h1><b>GOOD JOB!</b></h1>"); } } else { document.write('<h2><p>Sorry. The number was ' + randomNumber + '.</p></h2>'); }

Hi Jose,

I have cleaned your code in case any other treehouse members are viewing this question. I will post an answer shortly:

// Original Code
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6) + 1;
alert(randomNumber);
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');

if (parseInt(guess) === randomNumber) {
    correctGuess = true;
    document.write("<h1><b>AWESOME JOB!</b></h1>");

} else if (parseInt(guess) < randomNumber) {
    var guessMore = prompt('Please try again! Go higher!');

    if (parseInt(guessMore) === randomNumber) {
        correctGuess = true;
        document.write("<h1><b>GREAT JOB!</b></h1>");
    }

} else if (parseInt(guess) > randomNumber) {
    var guessLess = prompt('Please try again! Go lower!');

    if (parseInt(guessLess) === randomNumber) {
        correctGuess = true;
        document.write("<h1><b>GOOD JOB!</b></h1>");
    }

} else {
    document.write('<h2><p>Sorry. The number was ' + randomNumber + '.</p></h2>');
}

2 Answers

Hi Jose,

Your code is contains two nested conditional (IF) statements. To display your message without too many changes to your code, you can add the following ELSE statements:

if (parseInt(guess) === randomNumber) {
    correctGuess = true;
    document.write("<h1><b>AWESOME JOB!</b></h1>");

} else if (parseInt(guess) < randomNumber) {
    var guessMore = prompt('Please try again! Go higher!');

    // Add else satement to display incorrect number message;
    if (parseInt(guessMore) === randomNumber) {
        correctGuess = true;
        document.write("<h1><b>GREAT JOB!</b></h1>");
    } else {
        document.write('<h2><p>Sorry. The number was ' + randomNumber + '.</p></h2>');
    }

} else if (parseInt(guess) > randomNumber) {
    var guessLess = prompt('Please try again! Go lower!');

    // Add else satement to display incorrect number message;
    if (parseInt(guessLess) === randomNumber) {
        correctGuess = true;
        document.write("<h1><b>GOOD JOB!</b></h1>");
    } else {
        document.write('<h2><p>Sorry. The number was ' + randomNumber + '.</p></h2>');
    }

} else {
    document.write('<h2><p>Sorry. The number was ' + randomNumber + '.</p></h2>');
}

This code can be refactored (optimised) with skills you will learn in upcoming videos so don't worry about getting to involved with this code.

If you would like an example though, please let me know.

Jose Esplana
Jose Esplana
12,503 Points

Hi Ross,

Thank you! for your help. Are you already working as a Developer? Do you have a portfolio?

Thanks again!

Hi Jose,

Sorry for a slow reply.

I'm working as a SharePoint consultant. I use a lot of HTML, CSS & JavaScript during the day but most of my work is on company intranets so I don't really have a portfolio.

I am trying to get into web design as a side business but it is more of a hobby.