While Loop exercise doubt

I just posted a doubt I had on a break statement exercise that was a more advanced version of this one. It just so happens that it gives the same error in this one and I don’t know how to go around it :sweat_smile:

Any thoughts?

Your editor currently has a variable declared target to which a random number between 0 and 10 will be assigned. Print it to the console.

Under it write a small program which will keep guessing numbers and attributing them to the variable guess and printing them until the number guessed is the same as the target.

var target = Math.floor(Math.random() * 11);
var guess = Math.floor(Math.random() * 11);

console.log("Target is " +target);
console.log("First guess is " +guess);

while (guess!==target) {
 
 guess = Math.floor(Math.random() * 11);   
    console.log('New guess is '+guess);
}

console.log('Yay! You\'ve done it, '+guess +' is right');

This gives the error: >>>>Code is incorrect
Make you you’re printing the value of guess to the console in the line below assigning it a new value

It seems a bit foolish that “assign, assign, newline, log, log” wouldn’t be acceptable. But I guess you need to be human to comprehend the code. At least my guess is that to help prevent false OKs the “evaluating code” is extremely strict. i.e. not a line below but the line below.

Try following the “rule” explicitly

var target = Math.floor(Math.random() * 11); 
console.log("Target is " +target); 
var guess = Math.floor(Math.random() * 11); 
console.log("First guess is " +guess); 

Nope, it gives the same error. I don’t think that is the problem I think it is something in the while loop

As I told you in the other thread, that code interpreter is not the smartest. But it’s telling you the exact issue…

Change

console.log('New guess is '+guess);

to

console.log(guess);
1 Like

You’re absolutely right, it didn’t even hit me that the problem was it couldn’t recognize the variable after that text. Thank you so much!

Hi gfazenda,

your code…

<script>
(function( d ) {
   'use strict';
var target = Math.floor(Math.random() * 11);
var guess = Math.floor(Math.random() * 11);

console.log("Target is " +target);
console.log("First guess is " +guess);

while (guess!==target) {
 
 guess = Math.floor(Math.random() * 11);   
    console.log('New guess is '+guess);
}

console.log('Yay! You\'ve done it, '+guess +' is right');
}( document ));
</script>

worked repeatedly for me in my test browsers…

code-works

coothead

Hi coothead,

Yes, it’s like DaveMaxwell said, my code interpreter apparently is not very smart and it only accepts a specific path. If it weren’t for him I’d still be banging my walls out of frustration :sweat_smile:

Dump your code interpreter, what ever that may be, and
use your browser’s Web Console instead. Job done. :rofl:

coothead

I can’t, these are exercises I need to do in order to enter an intensive bootcamp to be a fullstack developer :sweat_smile: It’s all part of their process, I have 2 days to do 59 exercises like this so I can enter. So far, it’s not going great :laughing:

This is the case for many code interpreters. I would expect this to happen to future code exercises :slight_smile: .

Perhaps you have missed the real point
of “their” tests. :rolleyes:

Their” real test is probably to see if you
are observant enough to to see that “their
code interpreter
” is flawed and to then
draw “their” attention to this anomaly. :biggrin:

coothead

One thing, if I have any other questions like this should I open another thread or keep it in this one and edit the title? I don’t want to flood the forum with questions like these

When in doubt it’s always best to start a new thread.

We don’t want people to have to wade through hundred of comments that are unrelated to what you’re wanting to know. You can always supply a link from the new thread to related ones, if need be.

4 Likes