0

My main idea is generating some kind of "random greetings" and the idea is to generate a random number, and depends on the number to display "random greeting". Here is my non working code:

<p class="status">Your random splash screen: <span id="greeting">You don't have one</span></p>
<script>
var x = Math.floor((Math.random() * 4) + 1);
if (x == 1) {
  document.getElementById("greeting").innerHTML = "Greeting 1";
} else if (x == 2) {
  document.getElementById("greeting").innerHTML = "Greeting 2";
} else if (x == 3) {
  document.getElementById("greeting").innerHTML = "Greeting 3";
} else if (x == 4) {
  document.getElementById("greeting").innerHTML = "Greeting 4";
}
</script>

I have no idea what's wrong in it, please help!

undefined
  • 1,019
  • 12
  • 24

1 Answers1

0

As you can see here the code works fine:

var x = Math.floor(Math.random() * 4);
var greetings = ['Greeting 1', 'Greeting 2', 'Greeting 3', 'Greeting 4'];

document.getElementById("greeting").innerHTML = greetings[x];
<p id="greeting">Nice to meet you!</p>
Dominic
  • 62,658
  • 20
  • 139
  • 163
  • Did you run the snippet? All we can do is help using the info provided, you'll have to be more detailed with where/how you're trying to use this! Log out `document.getElementById("greeting")` and what do you see? – Dominic Jan 13 '17 at 16:58
  • I am going to use this @ www.beon.ga while it is loading the website ;) – undefined Jan 13 '17 at 17:00
  • 1
    Just making some fixes – undefined Jan 13 '17 at 17:00