Javascript函数if语句

Javascript函数if语句

我正在做一个奇怪的单人游戏,我对它的功能有些犹豫。
(我添加了一个警报,所以我知道正在单击该图像)
因此,基本上字符3是奇数输出,字符1 + 2不是奇数输出。但是,当我单击字符1 + 2时,会出现与字符3相同的警报(奇怪的一声)。我试过为正确/错误的答案制作新功能,但是那也不起作用。它可能只是很小的东西,但我真的很固执,不胜感激。

这是HTML:
     
     
     
    
    
     
        

<div id ="characters">
<img src="boy__pirate.png" class = "character" id ="character1"      onclick="characterclicked(1);" >
<img src="girl__pirate.png" class = "character" id ="character2"   onclick="characterclicked(2);" >
<img src="pig__.png" class = "character" id ="character3" onclick="characterclicked(3);">
</div>


这是JS:

var oddoneout = new Array();
oddoneout[0] = {characterName:"character3", answer: 1};
oddoneout[1] = {characterName:"character6", answer: 2};
oddoneout[2] = {characterName:"character9", answer: 3};

oddoneout.sort(function() {return Math.random() -0.5;});


function characterclicked() {
if (oddoneout) {
    alert("You're right!")


}
}

最佳答案

var oddoneout = new Array();
oddoneout[0] = {characterName:"character3", answer: 1};
oddoneout[1] = {characterName:"character6", answer: 2};
oddoneout[2] = {characterName:"character9", answer: 3};

oddoneout.sort(function() {return Math.random() -0.5;});

function characterclicked(choice) {
// perform your checking here
if (oddoneout[choice-1].answer===2) {
    alert("You're right!")
    }
}

关于javascript - Javascript函数if语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29902873/

10-12 00:49