0

I have a set of javascript that asks for a number between 1 and 10,000 and then continuely tries to guess that number. I'd like to ensure that the computer does not guess the number more than once. i.e. the number of guesses should never be more than 10,000.

I've tried to do this with an array and a few functions but clearly am missing something.

var upper = 10000;
var userPick = parseInt(prompt('Pick a number between 1 and 10,000'));
var computerGuess = ;
var attempts = 0;
var guessed = [];


function getRandomNumber(upper) {
  return Math.floor(Math.random()*upper) + 1;
};

function testRandomNumber(){
    while(guessed.indexOf(getRandomNumber!==-1))
        guessed.push(getRandomNumber);
        computerGuess=getRandomNumber;
} else {
    getRandomNumber(upper)
}




while(computerGuess !== userPick){
computerGuess = getRandomNumber(upper);
attempts+=1;

}

document.write('<p>Your number was ' + userPick + '. The computer guessed this number after ' + attempts + ' attempts.</p>')
  • Your syntax is all messed up. Have you tried opening your console (hit F12 on Windows, Opt + Cmd + I on Mac) and checked for errors? – Mike Cluck May 26 '16 at 17:22
  • You have some issues in your code or copy/paste. You have a `while` loop with no parenthesis, an `else` without an `if`, `testRandomNumber`'s parentheses are all messed up.. Sorry i dont really know whats going on here – element11 May 26 '16 at 17:22
  • Create an array with all the numbers from 1 to 10,000. Shuffle that list. Keep track of an index to that array starting at 0. For each guess, take the item in the array at the index and then increment the index. Now you will guess all 10,000 numbers with no repeats. – Matt Burland May 26 '16 at 17:25
  • @MikeC thanks for the reply. Just one missed ; on line three though I'm gonna say that's not the only thing wrong. –  May 26 '16 at 17:25
  • @CuriousComSci It's not a missed `;`. You've got one but no value preceding it. Perhaps you should try `var computerGuess = -1;` or something. Then you'll see more syntax errors as you fix them. – Mike Cluck May 26 '16 at 17:26
  • 1
    Further to @MattBurland comment, [Fisher-Yates shuffle in JavaScript](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array). – Crescent Fresh May 26 '16 at 17:28
  • why don't you turn this around, fill an array and remove a random entry? with array.splice([random_Index], 1); ? It is probably faster on the long run –  May 26 '16 at 17:30
  • @Mark: It's better than randomly stabbing for guesses, but constantly resizing the array doesn't perform very well either. – Matt Burland May 26 '16 at 17:33
  • @MattBurland Hm. Good point. Looking at your answer which I just realized it's there, it looks just fine for me. If I was the asker, I would accept it. Take my +1 for mentioning the Fisher-Yates Shuffle ;) –  May 26 '16 at 17:38
  • Thanks guys. My code is a mess. my bad on that –  May 26 '16 at 18:11

3 Answers3

0

In pseudo-code:

let guess = an array of numbers from 1 to 10,000
shuffle guess (hint: use Fisher-Yates shuffle)
let index = 0

let notGuessed = false;
while (notGuessed && index < guess.length)
{
    let thisGuess = guess[index];
    notGuessed = IsThisGuessCorrect(thisGuess)
    index++
}
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

The above answer is good, but I'd argue that you don't even need an array of x amount of numbers. Shuffling becomes irrelevant if you have a 1/x probability of picking the right number anyway.

Just store the guess, and declare your lower and upper limits:

var guess = getUserGuess()
var min = 1
var max = 10000

Then count up or down, as you please:

while (true && min <= max) {
   min === guess ? alert('number is ' + min); return false; : min++;
}

The pseudo code may be wonky but I think this way you'd gain some performance without declaring a large array and then shuffling it.

blrzzzt
  • 194
  • 5
  • I think the idea is to guess at random and not just start guessing at 1 and keep going up. Otherwise if the user entered `1` it would get it right away every time. – Matt Burland May 27 '16 at 01:25
0

I just came up with a pretty cool solution that involves

Array.prototype

Here is a plunker that I made to show you https://plnkr.co/edit/RK2YdGUvsGIsQEl4yXlA?p=preview

Chase Lundell
  • 93
  • 1
  • 11