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) Working With Numbers The Random Challenge Solution

My game is written differently, but I think it still works. Can someone check it for me?

I still need to figure out how to delay the alerts. Here is my JavaScript:

alert('Let\'s play a game!');  // Alert 1st Program BEGIN.
console.log('BEGIN 1st Program.');  // Log 1st Program BEGIN.

var input = prompt('Choose a number.');  // Entered userNumber.
var userNumber = parseInt(input); // Convert userNumber.
console.log(userNumber); // Log userNumber.

alert('Now, think of a number in between 1 & the number you just chose.');
alert('And now, I\'ll try to guess the number you\'re thinking of.');

var randomNumberOne = Math.random() * userNumber + 1;  // Select randomNumberOne.
console.log(randomNumberOne); //Log randomNumberOne.

document.write('<h2>Was ' + parseInt(randomNumberOne) + ' the number you were thinking of?</h2>');

alert('Thanks for playing.');  // Alert 1st Program END.
console.log('END 1st Program.');  // Log 1st Program END.

alert('Let\'s play another game!');  // Alert 2nd Program BEGIN.
console.log('BEGIN 2nd Program.');  // Log 2nd Program BEGIN.

var input1 = prompt('Again, choose a number.');  // Entered bottomNumber.
var bottomNumber = parseInt(input1); // Convert bottomNumber.
console.log(bottomNumber); // Log bottomNumber.

var input2 = prompt('Now, choose a larger number.');  // Entered topNumber.
var topNumber = parseInt(input2); // Convert topNumber.
console.log(topNumber); // Log topNumber.

alert('Now, think of a number in between the 2 numbers you just chose.');
alert('And again, I\'ll try to guess the number you\'re thinking of.');

var randomNumberTwo = Math.random() * (topNumber - bottomNumber + 1) + bottomNumber;  // Select randomNumberTwo.
console.log(randomNumberTwo); //Log randomNumberTwo.

document.write('<h2>Was ' + parseInt(randomNumberTwo) + ' the number you were thinking of?</h2>');

alert('Thanks for playing.');  // Alert 2nd Program END.
console.log('END 2nd Program.');  // Log 2nd Program END.

2 Answers

Will Berlanga
Will Berlanga
18,825 Points

Your program works. I would change the alert of the range to the following to make it easier for the user to follow your question.

alert('Now, think of a number in between 1 & ' + userNumber + '.');

and the for the second prompt

alert('Now, think of a number in between ' + bottomNumber + ' and ' + topNumber + '.');

Your strings are much easier to understand. Thank you. Do you know any way I can delay the alerts, so they don't appear right away?

Henry Garmendia
Henry Garmendia
Courses Plus Student 11,762 Points

and I'll recommend to add your comment on top of your code but is up to you brother