Reputation: 61
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
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
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