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

not printing? been an hour of trialing and changing things..........

here's my code:

var upper = 10000;
var randomNumber = getRandomNumber(upper);
var guess; var attempts = 0;

function getRandomNumber(upper) {
    return Math.floor(Math.random() * upper) + 1;
}

while(guess !== randomNumber) {
    guess = getRandomNumber(upper);
    attempts += 1;
}

document.write("<p>The random number was " + randomNumber() + "<p>");
document.write("<p>The computer guessed " + attempts()+ "to get it right</p>");

please elaborate

3 Answers

Hi Ben,

Debalina is correct - in your document.write statements, you need to append the variables randomNumber and attempts, so no parentheses. Also, your second paragraph needs an extra word and space. Here's a codepen with the solution. You can see your code in action. I've also pasted it below:

var upper = 10000;
var randomNumber = getRandomNumber(upper);
var guess; var attempts = 0;

function getRandomNumber(upper) {
    return Math.floor(Math.random() * upper) + 1;
}

while(guess !== randomNumber) {
    guess = getRandomNumber(upper);
    attempts += 1;
}

document.write("<p>The random number was " + randomNumber + "<p>");
document.write("<p>The computer guessed " + attempts+ " times to get it right</p>");

Hi Ben,

In addition to the other answers describing your syntax errors, here's an alternative implementation of what you're trying to do that might prove more insightful. Let me know if you have any questions.

// get a random number between two numbers
// prompt user to guess the number within a certain number of tries
// if the guessed number is the right number, tell user they won
// if the guessed number is less than the right number, tell user to guess higher
// if the guessed number is greater than the right number, tell user to guess lower

var minimumNumber = 1,
    maximumNumber = 100,
    randomNumber = Math.floor(Math.random() * (maximumNumber - minimumNumber + 1)) + minimumNumber,
    guessCount = 0,
    maxGuesses = 10

while (maxGuesses > 0) {
    var guessedNumber = parseInt(prompt('Guess a number between ' + minimumNumber + ' and ' + maximumNumber + '!'))

    if (guessedNumber === randomNumber) {
        alert('You win! The number was ' + randomNumber + '. It took you ' + guessCount + ' guesses.')
        break;
    } else if (guessedNumber < randomNumber) {
        alert('Nope. Higher...')
    } else {
        alert('Nope. Lower...')
    }

    if (maxGuesses === 0) {
        alert('You lose! The random number was ' + randomNumber + '. It took you ' + guessCount + ' guesses.')
    }

    maxGuesses--
    guessCount++
}

Please try randomNumber instead of randomNumber() and attempts instead of attempts() in document.write()