skip to Main Content

I have try to make the random number for code in this form using snippet in chrome

var n = Math.random();
n = n * 6;
Math.floor(n) + 1;

but when i try to make it shorter like this

var n = Math.floor(Math.random() * 6) + 1;

seem like it won’t work for me (undefined), but again I have seperated like this

var n = Math.random();
n = Math.floor(n * 6) + 1;

It kinda work. What is the problem of the shorter form for this code?

2

Answers


  1. var n = 6;
    if you run this code on browser console, it print the return value of = operator and it is undefined.

    Login or Signup to reply.
  2. Welcome to stackoverflow.
    Please try to add put your code in the code snippet element.

    Like this:

    var n = Math.random();
    n = n * 6;
    n = Math.floor(n) + 1;
    
    console.log(n);

    And here your second code:

    var n = Math.floor(Math.random() * 6) + 1;
    console.log(n);

    As you can see, both are working like expected.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search