Jon Frank
Jon Frank

Reputation: 61

(Javascript) Random integer between the output of two functions

Hello I am working on getting a random whole number between the output of two functions my example is below, I do not get an error but it will not work.

function getRndInteger(...args) {
  const [low, high] = [Math.min(...args), Math.max(...args)];
  return Math.floor(Math.random() * (high - low)) + low + 103;
}
function age(h){
 var h = prompt ("How old are you?");
 return h;
}
function videogames(i){
 var i = prompt ("How many hours of video games have you played last month?");
 return i;
}
document.write (getRndInteger(age(h),videogames(i)));

I have to write it out this way because the age and videogames part must be in the form of a function, is this possible?

Upvotes: 0

Views: 27

Answers (2)

brk
brk

Reputation: 50326

When you are calling age(h),videogames(i) there is no h or i declared anywhere. So remove them as parameter

function getRndInteger(...args) {
  const [low, high] = [Math.min(...args), Math.max(...args)];
  console.log(low, high)
  return Math.floor(Math.random() * (high - low)) + low + 103;
}

function age() {
  var h = prompt("How old are you?");
  return h;
}

function videogames() {
  var i = prompt("How many hours of video games have you played last month?");
  return i;
}
document.write(getRndInteger(age(), videogames()));

Upvotes: 1

Get Off My Lawn
Get Off My Lawn

Reputation: 36341

The issue that I see that you are having is that you are passing a parameter to the functions then redefining the variable which isn't needed. You can remove passing a value to the functions, and then just return the values like you are currently doing.

Note: You might want to cast your prompt values to integers like I did below since a prompt returns a string.

function getRndInteger(...args) {
  const [low, high] = [Math.min(...args), Math.max(...args)];
  return Math.floor(Math.random() * (high - low)) + low + 103;
}

function age() {
  var h = prompt("How old are you?");
  return parseInt(h);
}

function videogames() {
  var i = prompt("How many hours of video games have you played last month?");
  return parseInt(i);
}

document.write(getRndInteger(age(), videogames()));

Upvotes: 3

Related Questions