Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I created a function that generates a random number which is contained inside the variable. I run the code on the console yet I get undefined?

function nextSequence() {
    var randomNumber = (Math.floor(Math.random() * 3) + 1);
}

i ran this on the console yet only got undefined i dont know what is wrong with this. any help is appreciated

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you want to use this number outside of the function, you need to return it from the body of the function and then call the function itself:

function nextSequence() {
  return Math.floor(Math.random() * 3) + 1;
}

const num = nextSequence();

console.log(num);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading