Javascript算法错误3



我已经写下了这段代码,就像一个有密码的小游戏一样,我希望如果他写对了密码,或者写了结束,那么它真的结束了,向我们展示了一条消息,但万一他写了其他东西或什么都没有,它继续问,但这并不完全正确。

 do{
                    var password = prompt("What is the password? Just give up by typing end, you won't find it, heheeh.")
                    if(password == "I11I1II1I"){
                        window.alert("Ok, ahha, so fun, going to the code, and searching for the password, yes yes yes good job, you won, yey")    
                    }
                    else if(password == "end"){
                        window.alert("Bye Bye, ehhe.")    
                    }
                    else{
                        window.alert("I don't know how you found me, but hey, you won't find the password, eehhehe.")
                    }
                }
                while((password == "") || (password != "I11I1II1I") || (password != "end"))

当逻辑错误时,您应该在密码""(password != "I11I1II1I" && password != "end")时提示

do{
                password = prompt("What is the password? Just give up by typing end, you won't find it, heheeh.")
                if(password == "I11I1II1I"){
                    window.alert("Ok, ahha, so fun, going to the code, and searching for the password, yes yes yes good job, you won, yey") ;   
                }
                else if(password == "end"){
                    window.alert("Bye Bye, ehhe.")  ;
                }
                else{
                    window.alert("I don't know how you found me, but hey, you won't find the password, eehhehe.");
                }
            }
            while((password == "") || (password != "I11I1II1I" && password != "end"))

您可以使用break (https://www.w3schools.com/js/js_break.asp(:

while(true) {
    var password = prompt("What is the password? Just give up by typing end, you won't find it, heheeh.")
    if(password == "" || password == null) {
          // No password, just break or do something else
          break;
    }
    if(password === "I11I1II1I") {
          window.alert("Ok, ahha, so fun, going to the code, and searching for the password, yes yes yes good job, you won, yey");
          break;
    }
    else if(password === "end") {
          window.alert("Bye Bye, ehhe.");    
          break;
    }
    else {
          window.alert("I don't know how you found me, but hey, you won't find the password, eehhehe.")
    }
}

最新更新