I am writing in to ask some help on the following exercise.
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.
This is what thought would get me there (i have tried a few other different things)
var target = Math.floor(Math.random() * 11);
console.log(target);
var guess=Math.floor(Math.random() * 11);
console.log(guess);
while (guess !== target) {
console.log('tough luck!');
guess = Math.floor(Math.random() * 11);
}
console.log('you da boss!');
But - I keep getting this error: The first line in your while’s block should assign a new value to the variable guess.
Other than that I see no reason why your code wouldn’t get the job done. If that change doesn’t work, could it be that they expect a function? Here is what I came up with if you want to go that route:
var target = Math.floor(Math.random() * 11);
console.log(`Target: ${target}`);
var guess;
function matchVariables(target, guess) {
while (guess !== target) {
guess = Math.floor(Math.random() * 11);
console.log(guess);
}
}
matchVariables(target, guess);
/*Example
Target: 3
4
1
7
9
3
*/
It would help to be able to look at the actual problem. Hope you figure it out!
@big_D
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
maybe you just need to start at the minimum possible value, and then just raise it by 1 at each iteration of the loop? in this case you will check all possible values of target, instead of randomly trying to guess it