0

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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, im very new to functions but this helps a lot!
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.