0

i tried a few ways to get a random number from 1 to 10 and all return undefined or NaN why ? here is what i tried

var num = Math.floor(Math.random * 10)

function getNum() {
  return Math.floor(Math.random * 10);
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);

both dosn't give a number when logged

3 Answers3

1

Math.random is a method, not a property, so you need to call it as Math.random(). You are also missing a semicolon after your first line.

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
kloddant
  • 1,026
  • 12
  • 19
  • 1
    Semicolons are optional in JS. I mostly don't type them. Upvoted nonetheless – Jeremy Thille Nov 10 '20 at 19:33
  • I hesitate to upvote this only because the statement "*You are also missing a semicolon...*" isn't particularly relevant nor true because of [Automatic semicolon insertion](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi). – esqew Nov 10 '20 at 19:34
  • 1
    @JeremyThille - It may be worth clarifying your comment for future visitors to this answer (while you still can) to reflect that semicolons are "optional" only under certain criteria ([relevant SO thread](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi)). There are situations where the presence of a semicolon (or lackthereof) will materially affect the interpretation of a given segment of JavaScript code. – esqew Nov 10 '20 at 19:34
  • your a life saver been staring at it for an hour –  Nov 10 '20 at 19:35
  • Yes I know, I pulled my hair out many times with a line finishing with `()` and the next starting with a `[` :) Semicolons are indeed _mostly_ optional. – Jeremy Thille Nov 10 '20 at 19:37
  • you dont need a ; semi colon between variables especily if you have them in 2 lines –  Nov 10 '20 at 19:37
  • You might not strictly "need" a semicolon in this particular circumstance because browsers will cover for you, but it is best practice, especially since there are circumstances where you will need a semicolon, and it is nice to have consistent nomenclature with those circumstances, instead of having and omitting semicolons at random. There are also instances where your code might work but look ambiguous to other readers, who might not know your intentions. jshint and jslint will also regard this as an error, and I think "use strict" picks it up as an error as well. – kloddant Nov 10 '20 at 21:10
  • Additionally, when you are using a minifier, you will need to have the semicolons in there. Some minifiers might insert them for you, maybe, but I would rather not leave it up to chance to let the minifier correctly interpret your code. – kloddant Nov 10 '20 at 21:11
1

You need to actually invoke Math.random if you intend for it to generate the random number (ie Math.random())

var num = Math.floor(Math.random() * 10)

function getNum() {
  return Math.floor(Math.random() * 10);
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);
mwilson
  • 12,295
  • 7
  • 55
  • 95
0

As stated by @kloddant Math.random is method so you forgot the parentheses (). So here's the snippet of how you can implement it

var num = Math.floor(Math.random() * Math.floor(10))   

function getNum() {
  return Math.floor(Math.random() * Math.floor(10));
}
var num2 = getNum();
console.log('num = ' + num + 'num2 = ' + num2);
Karimov
  • 349
  • 4
  • 8