mardi 7 avril 2015

How to check random conditions in if/else statemnts?

I am trying to code an if/else statement that writes html to a page depending on what option the randomNumber function generates from the myOptions array. I am not sure how to check the if condition as it always results to the else which writes "You lose!.....".


Goal: Options 1 and Options 2 win, Options 3 and Options 4 lose.


fiddle it.



function randomNumber(range) {
return Math.round( Math.random() * range );
}

var myOptions = ["Option 1", "Option 2", "Option 3", "Option 4"];
myOptions = myOptions[randomNumber( myOptions.length - 1)];
alert(myOptions);

if (myOptions <= 1) {
document.getElementById("message").innerHTML = "You win!";
} else {
document.getElementById("message").innerHTML = "You lose! " + myOptions + " was your pick";
}

Aucun commentaire:

Enregistrer un commentaire