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) Creating Reusable Code with Functions Random Number Challenge

Why does it show alert box 2 times?

Here is my code.

function quiz() {
a = parseInt(prompt("Type the lowest possible value"));
b = parseInt(prompt("Type the highest possible value"));
result = Math.floor(Math.random() * (b - a + 1)) + a; 
  return result;

}
quiz();

document.write("Your number is " + quiz());

edited code for formatting.

2 Answers

AH yeah I see what you are saying.

Your are calling the function quiz() twice

function quiz() {
a = parseInt(prompt("Type the lowest possible value"));
b = parseInt(prompt("Type the highest possible value"));
result = Math.floor(Math.random() * (b - a + 1)) + a; 
  return result;

}
quiz();  //you don't need to call quiz() here...you are just calling it and not doing anything with it

document.write("Your number is " + quiz()); //since you are calling quiz() here for use in your document.write statement

thank u)

you're welcome :)

You have 2 prompt statements.

a = parseInt(prompt("Type the lowest possible value"));
b = parseInt(prompt("Type the highest possible value"));

no, after these 2 times, it asks 2 times again. just try it and you will see