如果语句忽略其他选项Javascript

时间:2022-08-13 22:51:06

The If statement I created ignores the else option I place in it. The first three options work but if i put in something like (y,x,z), it will give me two random if statements, while still giving me the "sorry" message. After doing a lot of research I still cant figure out what I did wrong.

我创建的If语句忽略了我放在其中的else选项。前三个选项有效但如果我输入类似(y,x,z)的东西,它会给我两个随机的if语句,同时仍然给我“抱歉”的消息。经过大量的研究后,我仍然无法弄清楚我做错了什么。

 var questionB = new Array ();
var key1= ["x", "y", "z"];
var key2= ["y","z","x"];
var key3 =["z", "x", "y"];

window.onload = function() {
var eSelect = document.getElementById('question1');
var optOtherReason = document.getElementById('displayresponse');
var options = document.getElementsByTagName("option");



eSelect.onchange = function() {



    questionB.push(eSelect.value);
    var y= document.getElementById("answerTest");
    y.innerHTML= questionB;



    if (eSelect.selectedIndex ==0) {

        optOtherReason.style.display = 'block';
        }



    if (questionB.length>2) {

            document.getElementById('question1').style.visibility="hidden";
            document.getElementById('generateButton').style.display = "block";
        }
        }
        }



    generate= function () {

        for ( var i=0; i<1; i++)
        {
        if (questionB[i]==key1[i]) {
            alert("your drink is an orange vanilla protein shake")
            document.getElementById("result1").style.display= "block";      

            }
        else if (questionB[i]==key2[i]) {
            alert("your drink is a strawberry nut protein shake")
            document.getElementById("result2").style.display= "block";      


            }
        else if(questionB[i]==key3[i]){
            alert("your drink is a wild berry protein shake")
            document.getElementById("result3").style.display= "block";      

        }
        else {document.getElementById("result4").style.display= "block";}   
        document.getElementById("result5").style.display= "block";

        }//end of generate function

    }

    function reloadPage(){
window.location.reload();
 }

<select id="question1" name="question" style="display:block;">
<option value=""></option>
<option value="x">Reason1</option>
<option value="y">Reason2</option>
<option value="z">Otherreason</option>
<!--<option value="none">None</option>-->


If you did not see a choice here, you may search for other sites. Your result is a orange vanilla shake. click here for ingredients .

Your result is a strawberry nut shake. click here for ingredients .

Your result is a wild berry shake. click here for ingredients .

Sorry, please try again.

Please play again.

2 个解决方案

#1


1  

Else ifs are not too good to use. Try the switch instead(This makes the code much more cleaner and readable as well):

否则ifs不太好用。改为尝试切换(这使得代码更加清晰和可读):

    switch(questionB[i])
    case(key1[i]) {
        alert("your drink is an orange vanilla protein shake")
        document.getElementById("result1").style.display= "block";      

        }
    case(key2[i]) {
        alert("your drink is a strawberry nut protein shake")
        document.getElementById("result2").style.display= "block";      


        }
    case(key3[i]){
        alert("your drink is a wild berry protein shake")
        document.getElementById("result3").style.display= "block";      

    }
    default{document.getElementById("result4").style.display= "block";}   
    document.getElementById("result5").style.display= "block";

    }//end of generate function

Please check the syntax...not too sure if that is correct or not.

请检查语法...不太确定是否正确。

#2


0  

if u have same operand in multiple if statements then try to use switch instead of nested if else statement that would be better approach; like here questionB[i]

如果你在多个if语句中有相同的操作数,那么尝试使用switch而不是嵌套if else语句,这将是更好的方法;像这里问题B [i]

generate = function(){
    for( var i=0; i<1; i++){
        switch(questionB[i]){
            case key1[i]:
                alert("your drink is an orange vanilla protein shake");
                document.getElementById("result1").style.display= "block";
                break;
            case key2[i]:
                alert("your drink is a strawberry nut protein shake");
                document.getElementById("result2").style.display= "block";
                break;
            case key3[i]:
                alert("your drink is a wild berry protein shake");
                document.getElementById("result3").style.display= "block";
            default:
                document.getElementById("result4").style.display= "block";
        }
        document.getElementById("result5").style.display= "block";
    }
}

#1


1  

Else ifs are not too good to use. Try the switch instead(This makes the code much more cleaner and readable as well):

否则ifs不太好用。改为尝试切换(这使得代码更加清晰和可读):

    switch(questionB[i])
    case(key1[i]) {
        alert("your drink is an orange vanilla protein shake")
        document.getElementById("result1").style.display= "block";      

        }
    case(key2[i]) {
        alert("your drink is a strawberry nut protein shake")
        document.getElementById("result2").style.display= "block";      


        }
    case(key3[i]){
        alert("your drink is a wild berry protein shake")
        document.getElementById("result3").style.display= "block";      

    }
    default{document.getElementById("result4").style.display= "block";}   
    document.getElementById("result5").style.display= "block";

    }//end of generate function

Please check the syntax...not too sure if that is correct or not.

请检查语法...不太确定是否正确。

#2


0  

if u have same operand in multiple if statements then try to use switch instead of nested if else statement that would be better approach; like here questionB[i]

如果你在多个if语句中有相同的操作数,那么尝试使用switch而不是嵌套if else语句,这将是更好的方法;像这里问题B [i]

generate = function(){
    for( var i=0; i<1; i++){
        switch(questionB[i]){
            case key1[i]:
                alert("your drink is an orange vanilla protein shake");
                document.getElementById("result1").style.display= "block";
                break;
            case key2[i]:
                alert("your drink is a strawberry nut protein shake");
                document.getElementById("result2").style.display= "block";
                break;
            case key3[i]:
                alert("your drink is a wild berry protein shake");
                document.getElementById("result3").style.display= "block";
            default:
                document.getElementById("result4").style.display= "block";
        }
        document.getElementById("result5").style.display= "block";
    }
}