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 trialfairest
Full Stack JavaScript Techdegree Graduate 19,303 PointsAny input on this on this alternative solution combining assignments?
var minValue = 0;
var minValue = 0;
// Function
function rollTheDice(minValue, maxValue) {
return Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue ;
}
// Value prompt
minValue = parseInt( prompt('Please enter a minimum value') );
while ( isNaN(minValue) ) {
minValue = parseInt( prompt('Sorry, numbers only. Please enter a minimum value') );
}
maxValue = parseInt( prompt('Please enter a maximum value') );
if ( isNaN(maxValue) || maxValue <= minValue ) {
while ( isNaN(maxValue) ) {
maxValue = parseInt( prompt('Sorry, numbers only. Please enter a maximum value') );
}
while ( maxValue <= minValue ) {
maxValue = parseInt( prompt('Plase enter a number higher than ' + minValue ) );
}
}
// Call Function
alert('The number is ' + rollTheDice(minValue, maxValue)) + '.';
// Play Again?
if (confirm('Play again?')) {
location.reload();
}else {
alert("Thank you for playing.")
}
2 Answers
Steven Parker
231,269 PointsBy using two separate loops for testing value order and non-number, once the loop to test for the value order begins running, the input will no longer be checking if the input is not a number. To make sure both criteria are upheld, check them both in one loop:
maxValue = parseInt(prompt("Please enter a maximum value"));
while (isNaN(maxValue) || maxValue <= minValue) {
if (isNaN(maxValue)) {
maxValue = parseInt(prompt("Sorry, numbers only. Please enter a maximum value"));
} else {
maxValue = parseInt(prompt("Plase enter a number higher than " + minValue));
}
}
fairest
Full Stack JavaScript Techdegree Graduate 19,303 PointsThanks!