Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Non-treehouse project

Hello Developers,

I'm working on a sideproject wich is not related with a treehouse project. It's a hangman game. i have a little issue here. I made a fucntion wich checks the answer, everything is working pretty fine exept for the "while loop" part. I know I did something wrong, but I can't really figure out how to fix it.

(note: the language of the non code is dutch)

While the answer is not "yes" or "no" it should repeat the answerOfPrompt.

But I guess it keeps reminding the answer I gave it earlier since it keeps saying "antwoord met ja of nee" -> "reply with yes or no"

function checkAnswer(){

            if (input.value.toUpperCase() == rand.toUpperCase()) {
                alert("Goed geraden!");
                score += 1;
                console.log("score: " + score);
                var answerOfPrompt = prompt("wil je een nieuw spel spelen?");

                    //while the answer of the prompt is not "ja" or "nee" repeat showing the prompt
                    while(answerOfPrompt != "ja" || answerOfPrompt != "nee") {
                        if (answerOfPrompt === "ja") {
                            newGame();
                        } else if (answerOfPrompt === "nee") {
                            //close the game
                        } else {
                            alert("antwoord met ja of nee");
                            // open the answerOfPrompt again

                        }
                    }
            } else {
                alert("fout!");
                guesses += 1;
                console.log("guesses: " + guesses)

                //if guessed to many times, game over
                if (guesses >= 5) {
                    alert("You lose");
                }
            }

I probably found the answer myself. For people who wants to know what I used for the solution:

 while(answerOfPrompt != "ja" || answerOfPrompt != "nee") {
                        if (answerOfPrompt === "ja") {
                            newGame();
                        } else if (answerOfPrompt === "nee") {
                            //close the game
                        } else {
                            alert("antwoord met ja of nee");
                            // open the answerOfPrompt again
                           answerOfPrompt = prompt("want to play another game?");
                        }
                    }

I fergot to recall the prompt. silly mistake :D

Aurellio

It seems to be a common mistake, it's the second forum post this morning with the same exact problem and resolution. I wonder if somehow the lesson teaching while loops / user input combinations is missing something in its content to make it clear you have to keep asking for the input AND re-setting the variable to the new input

1 Answer

Your problem is with the last else statement. Think about what you're trying to accomplish with the user there, and then determine if you've used the write javascript method to achieve that goal.