当满足if语句要求时,我的显示中奖消息不会显示
//start check function
function check(){
if(box1 >= 1 && box1 <= 6 && box2 >= 1 && box2 <= 6 && box3 >= 1 && box3 <= 6)
{
//display wining message
document.getElementById('header').textContent = 'Congrats! You win 1 credit.';
//add one credit when you get 3 reds
creditCounter = creditCounter + 1;
}
if(box1 >= 7 && box1 <= 9 && box2 >= 7 && box2 <= 9 && box3 >= 7 && box3 <= 9)
{
//display wining message
document.getElementById('header').textContent = 'Congrats! You win 10 credit.';
//add 10 credits when you get 3 green
creditCounter = creditCounter + 10;
}
if(box1 == 10 && box2 == 10 && box3 == 10)
{
//display wining message
document.getElementById('header').textContent = 'Congrats! You win 100 credit.';
//add 100 credits when you get 3 blue
creditCounter = creditCounter + 100;
}
else{
//display losing message
document.getElementById('header').textContent = 'Sorry, please try again.';
}
}//END check() function
最佳答案
您缺少“else if”,因此对于满足获胜条件的情况,最后的“else”无论如何都会进行。
查看我的 fiddle :http://jsfiddle.net/E9CMA/2/
if(box1 >= 1 && box1 <= 6 && box2 >= 1 && box2 <= 6 && box3 >= 1 && box3 <= 6)
{
...
}
else if(box1 >= 7 && box1 <= 9 && box2 >= 7 && box2 <= 9 && box3 >= 7 && box3 <= 9)
{
...
}
else if(box1 == 10 && box2 == 10 && box3 == 10)
{
...
}
else{
...
}
关于javascript - 如果声明有效..?或不?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170803/