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

Shawn Denham
seal-mask
.a{fill-rule:evenodd;}techdegree
Shawn Denham
Python Development Techdegree Student 17,802 Points
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)

Shawn Denham
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree
Shawn Denham
Python Development Techdegree Student 17,802 Points

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