Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Bram Lo
Bram Lo
1,590 Points

The computer can 'guess' the same number twice right?

Since the computer's 'guessing' code is equal to that of generating a random number it looks like the computer can guess the same number multiple times, which doesn't seem like good guessing to me. Can you fix this with an if/else statement?

Bram Lo
Bram Lo
1,590 Points

I say 'twice' but it's actually any number of times.

6 Answers

I definitely see what you're saying, narrowing the numbers with each dice roll IS taking away from the randomness of the number generator. But in the exercise, the computer's task is to try and guess against the random number it's already created. So in the exercise, two things are happening. 1) a static number is generated once, then stored for the computer to guess against. 2) the computer then has to guess that number. Because the guessing function doesn't have direct access to the static number we've stored, it is indeed guessing and that's the intent here, at this stage. That's why Bram is saying it's not doing a very good job. If I guess 5 and the number is 10, on my next guess, I'm not going to guess five again. Setting up a system in order to avoid doing that does actually have a lot of really good applications, so it's not a misguided question, it's a very similar question to what machine learning is all about. It's just outside of the scope of this class.

This must be a different exercise than what I am thinking... The ones I've encountered on here so far were asking to generate a number for a user to guess... and by your explanation, this is for the computer to guess a stored number.

In that case... yes, the computer guessing twice for a static number is undesirable. It would be a whole different story if the number that it was trying to guess was not static, and each attempt to guess allowed for the number to change... then there would be no reason to prevent the computer from guessing the same number.

Given that the number IS static...and it is the COMPUTER attempting to guess the number, not the user... then yes these guys as well as your question are 100% correct... the computer guessing the same number would be undesirable and you've been pointed in the right direction.

Sorry for the confusion. :P

Hey Bram,

Math.random()'s result can be repeated multiple times, especially if you set the random number to generate a number between 1 and 3. But if it is a standard floated number, the chances are very slim.

I suppose you could create a conditional to state what happens if Math.random() match:

function checkMath(){
foo = Math.random();
bar = Math.random();
console.log("Foo =" + foo)
console.log("bar =" + bar)

if (foo === bar){
   console.log('foo and bar matched, ending...');
}
else{
  console.log('foo and bar do not match, continuing...');
}
}

// Check it out in the console by calling it!
checkMath();

Also, this is a great read for more on the topic of Math.random()

Hope this helps!

Bram Lo
Bram Lo
1,590 Points

Thanks for the reply Timothy!

Bram!

Actually, you're right, the computer is not guessing well at all, and we could fix this if we wanted to take this lesson out of the scope of just simple loops fairly simply, but with some consequences. One way we could fix this is to store every number we guess into a collection, but that gets pretty large pretty fast. Also, searching through that collection is going to be pretty intensive too. The first thought I'd have is to make an array of the numbers we've guessed, but that's a HUGE task looping through the array if we start guessing numbers between 1 and 1000. If we were going to keep track of every number we guessed, I'd make each number guessed a key in an object literal, so we could easily refer to the key, instead of looping through the whole array. A better solution would be to have the computer give itself feedback, namely high/low, so that if it guessed high, it could only guess everything below the number it guessed, and if it guessed low, then it could only guess higher, and narrow down the field of guesses. But we're just concerned with the speed of loops in this lesson. Happy coding!

Nicolas

Bram Lo
Bram Lo
1,590 Points

Thanks for your answer Nicolas! The idea seems pretty neat though I haven't had enough object practise to put this to work. I'll keep following the track until I learn a little bit more about objects & arrays.

Bram

Pick up a pair of dice. Roll them. There are only 6 numbers on each standard die... and that means that the odds of a number repeating are high.

This isn't an issue of the computer "guessing poorly" or anything of the sort. Agreed... there are ways to force the computer to not guess the same number twice in a row, but then you are manipulating results...and then the already shaky 'randomness' of the numbers selected is in fact further diluted.

If you want to decrease the chances of the computer guessing the same number twice or multiple times in a row or in a given set of "tries" without unfairly manipulating the results... then just increase the number range that it is allowed to guess.

Bram Lo
Bram Lo
1,590 Points

Hi there Brandon,

Thanks for your reply. The challenge was to create code where the computer guessed a number. I noticed that it was possible for the computer to guess the same number. If I'm trying to guess a number I'm not guessing the same false guessed number twice.

With the bigger range, yeah the chance gets slimmer but the problem preexists.

Bram Lo
Bram Lo
1,590 Points

Btw sorry but I don't fully understand the manipulating results part.

OH. I think in that course it was having the computer generate a random number... not "guess" one. The guessing will be up to the individual.

You will see later in the course where the instructor shows you (through if/else at that point, if I remember correctly) how to give the user a second chance to guess at the same number.

** What I mean by manipulating results:

Doing what you were asking, keeping the computer from generating or guessing the same number twice in a given set of tries is like marking off a side of the dice each time a number comes up.... then forcing a re-roll of that die. This essentially takes away the randomness of the die roll.

Again, the thing to remember is that you are not setting the computer to "guess" ... rather you are setting it to GENERATE a number... so the number being repeated is not necessarily undesirable.

Think about this.... Paper - Rock - Scissor .... How many fingers do I have held up behind my back? .... which cup is the marble under.... etc.... Throwing out rock twice in a row could be repeated without it being an error... Holding 3 fingers up back to back is strategy. Although the computer is not using logic like that, it is still a naturally occurring repetition. So on and so on with other guessing games...

The most important part to grasp is that ...... the computer is not guessing... it is generating for someone else to guess.

Bram Lo
Bram Lo
1,590 Points

Yes it was the course before the one where the user has to type in a guess. Thanks for clarifying the manipulating part, it's starting to get more clear.

You're quite welcome. The JavaScript courses, as I'm sure you see, are very well constructed and taught very well. Naturally, more and more will be revealed and explained as it goes along.

Igor Protsenko
Igor Protsenko
15,651 Points

I think i've tackled this problem with an array https://w.trhou.se/hvctug4olj