Random numbers


#1

6. Random

This is a piece of code that I have written. It is not what the instructions asked for but nevertheless, the output in the console said undefined. When I called the function, I expected it to output a random number from 1 to 100. What went wrong?

function random(number1, number2) {
  Math.floor(Math.random() * ((number2-number1) + number1));
console.log(random(1, 100));

#2

here:

function random(number1, number2) {

the { has no matching closing curly bracket

you forgot to make your function return the random generated number, so currently nothing is happening with your randomly generated number and the function returns the default (guess what that is? Good guess, undefined indeed)


#3

@stetim94 Thanks! As you can see, I am still a beginner.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.