I'm working on a guessing game-- regular and reverse. My problem lies within the reverse game. User enters min and max to provide a range for computer to then guess a number that the user has chosen (mentally). As the computer guesses, the randomNum is then stored in an array.
User clicks yes if a match (this works), or "no guess lower" if randomNum is too high or "no guess higher" if randomNum is too low.
For the 'no' options, I need to compare the randomNum to the array to check if the randomNum was already generated and added to the array. It does this, but, it will still render to the app the duplicate randomNum as a guess.
What logic am I missing to prevent this behavior? To be able to just eliminate/ignore from rendering the duplicate randomNum as a guess?
I've searched for a solution, but the solutions are pertaining to having values in an array to compare to, which I seem to be doing.
I'm searching for the logic to take for when there is a duplicate randomNum:
- how to have the code re-generate another randomNum
- ignore the duplicate randomNum and not render it to page as a guess.
- preferably with vanilla javascript
here is jsfiddle of the code: GuessingGame
the function(s) in question are for computerGuessAgainHigher
and computerGuessAgainLower
minValue = oldRandom;
console.log("oldRandom", oldRandom, "minValue", minValue, "maxValue", maxValue);
generateRandomNum(minValue, maxValue);
console.log(randomNum, "in guessHigher");
if(randomNum === oldRandom) {
}
if(numbersGuessed.includes(randomNum) === false){
numbersGuessed.push(randomNum);
guessResponse2.textContent = "Is your number " + randomNum + " ?";
} else{
console.log("number already guessed- in guessHigher");
console.log("random in computerGuess", randomNum);
guessResponse2.textContent = "Is your number " + randomNum + " ?";
}
console.log("randomNum", randomNum);
console.log("numbersGuessed(too low): ", numbersGuessed.join(", "));
}
function computerGuessAgainLower() {
maxValue = oldRandom;
console.log("oldRandom:", oldRandom, "minValue:", minValue, "maxValue:", maxValue);
generateRandomNum(minValue, maxValue);
console.log(randomNum, "in guessLower");
if(randomNum === oldRandom) {
}
if(numbersGuessed.includes(randomNum) === false){
numbersGuessed.push(randomNum);
guessResponse2.textContent = "Is your number " + randomNum + " ?";
} else {
console.log("number already guessed- in guessLower");
console.log("random in computerGuess", randomNum);
guessResponse2.textContent = "Is your number " + randomNum + " ?";
}
console.log("randomNum: ", randomNum);
console.log("numbersGuessed(too high): ", numbersGuessed.join(", "));
}
Thank you and let me know if I need to amend my question in any way