When entering a letter that is in the word variable, according to the idea, if there is no such letter, then it should display a message, that there is no such letter .

But for some reason this message is displayed even if this letter is there, why?

If you remove the last condition, it works fine

https://jsfiddle.net/xqqbyexb/

var word = 'ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°'; var answerArray = []; for (var i = 0; i < word.length; i++) { answerArray[i] = "_"; } var remainingLetters = word.length; while (remainingLetters > 0 ) { alert(answerArray.join(" ")); var guess = prompt("Π£Π³Π°Π΄Π°ΠΉΡ‚Π΅ Π±ΡƒΠΊΠ²Ρƒ, ΠΈΠ»ΠΈ Π½Π°ΠΆΠΌΠΈΡ‚Π΅ ΠžΡ‚ΠΌΠ΅Π½Π° для Π²Ρ‹Ρ…ΠΎΠ΄Π° ΠΈΠ· ΠΈΠ³Ρ€Ρ‹."); if (guess === null) { // Π’Ρ‹Ρ…ΠΎΠ΄ΠΈΠΌ ΠΈΠ· ΠΈΠ³Ρ€ΠΎΠ²ΠΎΠ³ΠΎ Ρ†ΠΈΠΊΠ»Π° break; } else { for (var j = 0; j < word.length; j++) { if(answerArray[j] === guess){ alert('Вакая Π±ΡƒΠΊΠ²Π° ΡƒΠΆΠ΅ Π΅ΡΡ‚ΡŒ') } else if(word[j] === guess) { answerArray[j] = guess.toLowerCase(); remainingLetters--; } else if(word[j] != guess){ alert('Π’Π°ΠΊΠΎΠΉ Π±ΡƒΠΊΠ²Ρ‹ Π½Π΅Ρ‚Ρƒ'); break; } } } } 

    1 answer 1

    You exit the loop using break; , only when the letter that the user entered is not equal to the letter we are considering. Thus, the user must guess letters in turn, starting with the first.

    For example, the hidden word is Π°Π±Π² , and the user enters the letter Π° . First, we compare Π° and Π° , and since they are equal, then the first character in answerArray becomes letter Π° . Further, Π° and Π± compared, and they are not equal, it seems Π’Π°ΠΊΠΎΠΉ Π±ΡƒΠΊΠ²Ρ‹ Π½Π΅Ρ‚Ρƒ and the following letters will not be checked.

    But, actually, the corrected version: https://jsfiddle.net/xqqbyexb/10/ . Now everything works, and at the end the hidden word is displayed;)

    • in any sequence - DivMan
    • @DivMan corrected the answer) - Semushin Sergey