'Why does Math.floor(Math.random()) function always return "0"?

I am writing a program that chooses a random number between 0 and 1 and then enters a while loop until the random number generator selects a value more than .5. Every time I run the program, the program returns 0 and loops infinitely until it crashes. Why is this occurring? Shouldn't Math.floor(Math.random()) eventually select a number higher than .5?

var randomNumber = Math.floor(Math.random());
while(randomNumber < .5) {
var name = prompt("Do you want to play a game? Like checkers or something?");
if (name === "Yes") {
    console.log("Good jorb!");
} else if(name === "No.") {
    console.log("Go away!!!!!");
 else {
    console.log("I have no idea");
}

var randomNumber = Math.floor(Math.random());
}


Solution 1:[1]

Your variable randomNumber was already initialized with your first line, to change it's value simply use randomNumber = newValue where newValue is the value you wish to set it to, using a method or hardcoded value. You do not need to use the var keyword again.

Also using Math.floor method on Math.random will always return 0, as Math.random will return a number between 0 and 1, which will floor to 0.

You were missing the closing bracket on your while loop.

I cleaned you code up a little to chain your if boolean operators, although there are better ways to construct this code.

var randomNumber = Math(Math.random());
while(randomNumber > .5) {
    var name = prompt("Do you want to play a game? Like checkers or something?");
    if (name === "Yes" || name === "yes" || name === "Yes." || name === "yes." || name === "Yup" || name === "Yup." || name ===  "yup." || name === "yup")
    {
        console.log("Good jorb!");
    }

    else if(name === "No." || name === "No" || name === "no" || name === "no." || name === "nope" || name === "nope." || name ===  "Nope" || name === "Nope.")
    {
        console.log("That's too bad.");
    }
    else 
    {
        console.log("I don't know, man. I don't know");
    }
    randomNumber = Math(Math.random());
};// Close your while loop.

Solution 2:[2]

Your while loop will never run.
Math.random() returns a number n where 0 <= n <1
Math.floor(n) returns n rounded towards zero.

so your variable randomNumber will always equal zero.

you could also replace your if statements with an array of values to check.
Then look up the index of name in that array.
if the index is -1 it doesn't exist otherwise log "Good jorb!"

var randomNumber = Math.random();
while(randomNumber > .5) {
  var name = prompt("Do you want to play a game? Like checkers or something?");
  var yesArray = ["Yes", "yes", "Yes.", "yes.", "Yup", "Yup.", "yup.", "yup"];
  if(yesArray.indexOf(name) == -1) {
    console.log("I don't know, man. I don't know");
  } else {
    console.log("Good jorb!");
  }
  randomNumber = Math.random();
}

Solution 3:[3]

There are several reasons:

  • Math.floor(Math.random()), will always be zero so the loop will never start. Math.random() will give a number between 0 and 1 and you are flooring that one that means you are always rounding off down, which means zero.
  • If you want the while to stop, it is better to break; when the condition is right.

This code will work:

var randomNumber = Math.random();
while(randomNumber > .5) {
    var name = prompt("Do you want to play a game? Like checkers or something?");
    if (name === "Yes") {
        console.log("Good jorb!"); break;
    } else if(name === "Nope.") {
        console.log("Okay that is fine.");
    } else {
        console.log("I don't know, man. I don't know");
    }
    randomNumber = Math.random();
}

But now it does not just depend on the answer whether the loop will continue but also on randomNumber.

Solution 4:[4]

Try this: (Sorry if I got any of the counts wrong on the long expletive... there were a lot of them...)

while(true){
    var name=prompt("Do you want to play a game? Like checkers or something?");
    if(name.search(/^[yY](es|up)\.?$/)!=-1){
        console.log('Good jorb!');
    }else if(name.search(/^[nN]o(pe)?\.?$/)!=-1){
        console.log('F'+'U'.repeat(12)+'C'.repeat(13)+'K'.repeat(9)+' Y'+'O'.repeat(11)+'U'.repeat(18)+'!'.repeat(12));
    }else{
        console.log("I don't know, man. I don't know");
    }
    if(condition){
        break;
    }
}

Just to clarify about the regular expressions: They essentially look for the different forms of the word with capitalization or no and punctuation or no representing the full word (^ is the beginning of the string and $ is the end of the string). That should save you a lot of trouble.

Oh, and try to avoid while(true) for real development practices. for(var i=0;i<2e7;i++) or something similar is a better practice, but i figure if it's just for command line, that should be fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Feathercrown
Solution 3 Saeed Zhiany
Solution 4