Anthony DeGennaro
Anthony DeGennaro

Reputation: 99

Why isn't my math.random function working?

Here's the code I'm running:

var random = math.random();

function randomTest() {
if (random<0.33) {
    document.write("first")
}
else if (random<0.40) {
    document.write("second")
}
else if (random<0.80) {
    document.write("third")
}
else {
    document.write("last")
}
}
document.write(randomTest());

Just trying to test out some things with math.random and functions, as I'm a beginner, and wondering why nothing is being written to the document when I run this. Any help is appreciated, much thanks.

Upvotes: 0

Views: 148

Answers (1)

Brant Olsen
Brant Olsen

Reputation: 5664

Its Math with an uppercase M. Also, you do not need to do document.write(randomTest()) as randomTest() does not return anything.

var random = Math.random();

function randomTest() {
  if (random < 0.33) {
    document.write("first");
  } 
  else if (random < 0.40) {
    document.write("second");
  } 
  else if (random < 0.80) {
    document.write("third");
  } 
  else {
    document.write("last");
  }
}
randomTest();

Upvotes: 3

Related Questions