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
Related posts
- Convert Wiimote MAC Address to PIN from C++ to Python or Javascript
- Call Python function from JavaScript code
- Web-scraping JavaScript page with Python
- Postpone code for later execution in python (like setTimeout in javascript)
- Conversion from JavaScript to Python code?
- How to escape single quotes in Python on a server to be used in JavaScript on a client
- JSON.stringify (Javascript) and json.dumps (Python) not equivalent on a list?