1

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
4

2 に答える 2

3

「else if」が抜けているので、勝利条件が満たされている場合は、最後の「else」がとにかく続行されます。

私のフィドルを参照してください: 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{
...
}
于 2013-10-03T23:44:46.903 に答える
1

基準が満たされたら、チェックを停止する必要がありreturnます。たとえば、.. を使用してこれを行います。

//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;

        return; // they won 1 credit.. finish checking
    }
    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;

        return; // they won 10 credits.. finish checking
    }
    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
于 2013-10-03T23:37:54.607 に答える