0

The issue I am having with the below code is that it yields "1" or "January" each time instead of, I guess, using the randomly generated number between 1-12. My initial thought is that the issue is that some piece is not allowing the Math.random method to properly execute, so it is defaulting to 1? The Math.random function works perfectly until I add the "if" statements. I think I may be calling them incorrectly.

note: I believe I could create a loop or something to return the corresponding month, but I am trying to keep the code as basic as possible as I am still learning the fundamentals.

Any help or insight is very welcome. Thanks!

function randomNum(min, max) {
  var result = Math.floor(Math.random() * (max - min + 1) + min);
  if (result = 1) {
    return "January";
  } else if ((result = 2)) {
    return "February";
  } else if ((result = 3)) {
    return "March";
  } else if ((result = 4)) {
    return "April";
  } else if ((result = 5)) {
    return "May";
  } else if ((result = 6)) {
    return "June";
  } else if ((result = 7)) {
    return "July";
  } else if ((result = 8)) {
    return "August";
  } else if ((result = 9)) {
    return "September";
  } else if ((result = 10)) {
    return "October";
  } else if ((result = 11)) {
    return "November";
  } else if ((result = 12)) {
    return "December";
  }
  return result;
}

console.log(randomNum(1, 12));

4 Answers4

2

You could take a factor of an array length for the random value and take the integer values as index for getting a month name.

function getRandomMonth() {
    const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

    return months[Math.floor(Math.random() * months.length)];
}

console.log(getRandomMonth());
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You are using assignment operator in if instead of equals check. Replace all the

=

With

==

In your conditions

gkrthk
  • 331
  • 1
  • 7
0

Use == or === (strict equality) to compare, not = (assignment).

function randomNum(min, max) {
  var result = Math.floor(Math.random() * (max - min + 1) + min);
  if (result == 1) {
    return "January";
  } else if ((result == 2)) {
    return "February";
  } else if ((result == 3)) {
    return "March";
  } else if ((result == 4)) {
    return "April";
  } else if ((result == 5)) {
    return "May";
  } else if ((result == 6)) {
    return "June";
  } else if ((result == 7)) {
    return "July";
  } else if ((result == 8)) {
    return "August";
  } else if ((result == 9)) {
    return "September";
  } else if ((result == 10)) {
    return "October";
  } else if ((result == 11)) {
    return "November";
  } else if ((result == 12)) {
    return "December";
  }
  return result;
}

console.log(randomNum(1, 12));

You can simplify this by simply retrieving random elements from an array of all the months.

function randomMonth(){
  return ["January", "February", "March", "April", 
      "May", "June", "July", "August", "September", 
      "October", "November", "December"][Math.random() * 12 | 0];
}
console.log(randomMonth());
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

Your code block tries to use = for comparison in cases like this if (result = 1). result = x is an assignment operation which not only assigns x to result but also returns x. Since, the general rule for if with non-boolean is that the value returned should not be null for execution, it results in execution of if block. == is used for comparison.

Try using a list with all the months in it, and then just index the list based on the random number generated :)

function randomNum(min, max) {
  let result = Math.floor(Math.random() * (max - min + 1) + min);
  let months= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  
  return months[result-1];
}
console.log(randomNum(1, 12));

*Use let to define variable to strictly restrict their definition within a {} block or function. This helps avoid unintentional change of global variables from within a function.

Sarwagya
  • 166
  • 11