Attempting to create an if/else
statement that dynamically generates HTML content based on the option selected by the randomNumber
function from the myOptions
array. Struggling with checking the if condition, as it consistently falls into the else block displaying "You lose!...".
Aim: Options 1 and Options 2 result in a win, while Options 3 and Options 4 lead to a loss.
Check out the code snippet here.
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 choice";
}