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, Part II Solution

Jaroslav Zbortek
Jaroslav Zbortek
3,470 Points

How 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

Jaroslav Zbortek
Jaroslav Zbortek
3,470 Points

Hi, could you please elaborate more how it should be used in this example, cant make it work :(

there is my code:

while (isNaN) {

  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);

  if (isNaN=false) {
    break;
  }
}  

  document.write(result);

Thanks!

Jaroslav Zbortek
Jaroslav Zbortek
3,470 Points

Hi,

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:

var result = 0;

var minValue;
var maxValue;

minValue = parseInt(prompt("Please enter minimal numerical value."));
maxValue = parseInt(prompt("Please enter maximal numerical value."));

result = randomNumberGenerator(minValue, 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;
    }
}

while ( result === undefined ) {
minValue = parseInt(prompt("Please enter minimal numerical value."));
maxValue = parseInt(prompt("Please enter maximal numerical value."));

result = randomNumberGenerator(minValue, maxValue);
}


document.write(result);

Thanks for advice!

Now i just would like to find out how to not write twice same code:

minValue = parseInt(prompt("Please enter minimal numerical value."));
maxValue = parseInt(prompt("Please enter maximal numerical value."));

as i have it at begging of code and then again in while loop, any ideas??

Jaroslav Zbortek
Jaroslav Zbortek
3,470 Points

If 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);