Ошибка алгоритма Javascript 3
Я записал этот код, как мини-игру с паролем, и я хочу, чтобы, если он пишет пароль правильно или пишет end, то он действительно заканчивается, показывая нам сообщение, но в случае, если он пишет другие вещи или ничего такого, что продолжает спрашивать, но это не совсем правильно.
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"))
2 ответа
Решение
Вы, хотя логика неверна, вы должны запросить, когда пароль ""
или же (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.")
}
}