Javascript Psychic Game

Javascript Psychic Game

If you created an array of the users answers so far

var userAnswers = [];

and made it so that whenever you answered, it would check all the previous answers to see if it matched one.

var isDuplicate = false;
for (var i = 0; i < userAnswers.length; i++) { //for every previous answer
    if (currentAnswer == userAnswers[i]) { //check if the current answer is a previous one
        isDuplicate = true;
        break; //exit loop
    }
}
if (!isDuplicate) { //different than previous
    userAnswers[userAnswers.length] = currentAnswer;
    //do other code here
}

I would use the array.includes method to check if guessChoices includes the users current guess, would look something like this:

if (options.indexOf(userGuess) > -1) {
    if (userGuess === computerGuess) {
        wins++;
        numGuesses = 9;
        guessChoices = [];
    }
    else {
        if (guessChoices.includes(userGuess)) {
            // Code for duplicate guess
        }
        else {
            numGuesses--;
            guessChoices.push(userGuess);
        }
    }

    if (numGuesses === 0) {
        numGuesses = 9;
        losses++;
        guessChoices = [];
    }
}   

However you are resetting the computers guess on every keypress so it may give some unintended results.

Javascript Psychic Game

Leave a Reply

Your email address will not be published.