I am working on an assignment and I am having difficulty writing up the function to get a random number between two variables. Basically what I want is the script to prompt you for the first number followed by the second then give me a random number in between those two. How do I get a random whole number in between two user inputted variables? Now I will show you the code: <!DOCTYPE html> <html> <body> <script> var age = prompt ("How old are you?"); var videogames = prompt ("How many hours of video games have you played last month?"); function getRndInteger(age, videogames) { return Math.floor(Math.random() * (videogames - age)) + age; } document.write (getRndInteger(age,videogames)); </script> </body> </html> Code (JavaScript):
Not sure if this is what you want: https://jsfiddle.net/h0xftysu/ <html> <body> <script> var age = prompt ("How old are you?"); var videogames = prompt ("How many hours of video games have you played last month?"); function getRndInteger(age, videogames) { return Math.floor(Math.random() * (videogames + age)); } document.write (getRndInteger(age,videogames)); </script> </body> </html> Code (markup):
That doesn't appear to be working I tested age as 1 and video games as 10 and it outputted 56, no idea how. What did work for me is function getRndInteger(...args) { const [low, high] = [Math.min(...args), Math.max(...args)]; return Math.floor(Math.random() * (high - low)) + low; } var age = prompt ("How old are you?"); var videogames = prompt ("How many hours of video games have you played last month?"); document.write getRndInteger(age,videogames)); Code (JavaScript):