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 trialJaroslav Zbortek
3,470 PointsHow to re-ask user for new values
Hi,
i have tried to do this challenge in my own way(random number generator which generates numbers between values set by user), there is my code:
var result = 0;
function randomNumberGenerator(min, max) { if ( isNaN(min) || isNaN(max) ) { alert("Error, both values must be numeric values!"); } else { return Math.floor(Math.random() * (max - min + 1)) + min; } }
var minValue = parseInt(prompt("Insert minimal numeric value.")); var maxValue = parseInt(prompt("Insert maximal numeric value."));
result = randomNumberGenerator(minValue, maxValue); document.write(result);
It works, but i would like to know way how to ask user with prompt again if he will use non numeric values, because right now whole program ends with alert message.
Thanks a million :)
2 Answers
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsTry wrap it in a while-loop :-)
Jaroslav Zbortek
3,470 PointsIf is still someone interested how i did, there is the final result!
var result = 0;
var minValue;
var maxValue;
function randomNumberGenerator(min, max) {
if ( isNaN(min) || isNaN(max) ) {
alert("Error, both values must be numeric values!");
} else {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}
do {
minValue = parseInt(prompt("Please enter minimal numerical value."));
maxValue = parseInt(prompt("Please enter maximal numerical value."));
result = randomNumberGenerator(minValue, maxValue);
}
while ( result === undefined ) {
}
document.write(result);
Jaroslav Zbortek
3,470 PointsJaroslav Zbortek
3,470 PointsHi, could you please elaborate more how it should be used in this example, cant make it work :(
there is my code:
Thanks!
Jaroslav Zbortek
3,470 PointsJaroslav Zbortek
3,470 PointsHi,
if you are interested there is the result :) after a while i made it work, needed to brake the code to basics and put it back together again:
Thanks for advice!
Now i just would like to find out how to not write twice same code:
as i have it at begging of code and then again in while loop, any ideas??