- You’re comparing literal string rather than variables.
- You need to call the function
selectDoor()
to get the random index. The variablerandomDoor
scope is closed to selectDoor’s block.
$(document).ready(function() {
let doors = ["door1", "door2"]
function selectDoor() {
const randomDoor = doors[Math.round(Math.random())]
return randomDoor
}
const $door1 = $('.door1')
const $door2 = $('.door2')
$door1.click(function() {
const door1Selection = $door1.attr('class');
if (door1Selection === selectDoor()) {
alert("Yay you win")
} else {
alert("Boo you lose")
}
})
$door2.click(function() {
var door2Selection = $door2.attr('class');
if (door2Selection === selectDoor()) {
alert("Yay you win")
} else {
alert("Boo you lose")
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="door1">button 1</button>
<button class="door2">button 2</button>
0
solved Javascript – help debug simple game?