0

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:

  1. how to have the code re-generate another randomNum
  2. ignore the duplicate randomNum and not render it to page as a guess.
  3. 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

Lee
  • 41
  • 6
  • You have to store each generated number to the array, so you can look them up when a new number is generated. If the generated number was a duplicate, you should generate a new number and repeat as long as there are available numbers between min/max. – Bojan Bedrač Feb 05 '20 at 21:30
  • 1
    Why would the computer guess randomly? Use a more clever strategy :-) – Bergi Feb 05 '20 at 21:32
  • The binary search algorithm is your friend. Also, since the human can cheat it might be better to ask them for the min, max and the actual number. Just don't use the number when guessing. Though if binary search is done correctly you can prevent them from cheating too much. – Dave S Feb 05 '20 at 21:34
  • can you post a minimal code your tried withe – Vishnu Feb 05 '20 at 21:36
  • 1
    This was a bit quick on the close trigger, wasn't it? Especially since it is not a duplicate of https://stackoverflow.com/questions/11808804/generate-unique-number-within-range-0-x-keeping-a-history-to-prevent-duplic – Luc Feb 05 '20 at 21:37
  • yes I am storing each generated number to the array and am checking if the array includes any randomNum generated. Bergi, point taken :) , once I fix this problem I will amend the code to change the strategy. – Lee Feb 05 '20 at 21:43
  • Dave, yes, my thoughts as well, the task doesn't dicate for the user to enter their chosen number-- once I solve my original issue, I can then add on checks for the code to make to reason out if the user is changing their number on it. My code does check for duplicates, what I'm failing to see is..how to prevent the code from grabbing that duplicate and rendering it to the app as a guessed number for user to decide too high or too lower to trigger computer to guess again. – Lee Feb 05 '20 at 21:43
  • Vishnu, can you clarify what you'd like me to post? Thank you – Lee Feb 05 '20 at 21:44
  • Bergi, can my question be re-opened? My question isn't a duplicate to the one referred to. I amended my question title to hopefully reflect the difference for more clarity. – Lee Feb 05 '20 at 21:47
  • 1
    @Lee, you don't have to store your previously guessed numbers. Since the user tells you which way to guess next, you can just adjust your min and max values until you find the correct number. See https://jsfiddle.net/36sqgeav/6/ – Luc Feb 05 '20 at 22:15
  • @Luc, yes I understand its not needed persay to narrow the guess down, but as per the instructions, those values will be rendered once a win or lose occurs-- code is to render all guesses made. Which tho, actually has me wondering... if a guess has been made it should be then set as either the new minValue or maxValue... technically it can be 'guessed again', which still renders to the page as a guess, which isn't wanted -- I'm not sure where in my logic of my code I'm allowing the duplicate to render and how to eliminate that. At moment I'm not seeing where I'm permitting it. – Lee Feb 06 '20 at 00:57
  • 1
    OK, I've taken another look. it seems you are setting your new min/max values incorrectly. It should be 'minValue = oldRandom + 1;' and 'maxValue = oldRandom - 1;'. Your random function includes min and max bounds in the guess, so you get duplicates even when you adjust the bounds to last guess made. – Luc Feb 06 '20 at 07:48
  • @Luc ah I see that! yes since after the initial minValue and maxValues being set, they are then set from oldRandom --already guessed. Thank you for the guidance – Lee Feb 06 '20 at 18:05

0 Answers0