Loop in JavaScript bootcamp exercise

Hi iam stuck in here for tooo long, can anybody enlight me , with some directions ?

the challenge is:

You know that your target’s password is 4 characters long, so you’ll just have to brute force 1 character at a time. We already declared the variable correctGuesses which you should use to keep track of how many characters you have guessed so far.

Bear in mind that your program does not need to guess the password, that is not your goal!

You need to create a loop that only stops when all 4 characters have been guessed. On each loop iteration you need to calculate a random number between 1 and 3, which will correspond to each of the bellow scenarios

You guessed one character correctly, which increases correctGuesses by 1 and prints the message ‘Found X characters’ (where X is replaced with the current number of correct guesses).

You guessed incorrectly and your target’s terminal has detected too many attempts, which resets correctGuesses to 0 and prints the message ‘Starting over’ to the console.

You guessed incorrectly, but have not been detected yet, correctGuesses is kept with the same value.

Once the password is cracked (that is, correctGuesses has a value of 4) you should print the message ‘Terminal hacked!’.

Make sure all the messages in your code are in the correct format in order to advance!

//my code:


var correctGuesses = 0;

var password = 4;

while (correctGuesses < password) 
{ var guess = Math.ceil(Math.random ()*3); 

if (guess === 1) 
{correctGuesses++;
console.log('Found'+correctGuesses+'characters'); }

else if (guess===2)
    { correctGuesses=0;
    console.log('Starting over'); }

else if (guess===3)
{ correctGuesses=correctGuesses;
}
 }

if (correctGuesses === password)
 { console.log('Terminal hacked!'); }

//output:


>>>>Code is incorrect

You guessed some characters correctly, but failed to do the right thing!

Found1characters

Found2characters

Starting over

Starting over

Starting over

Found1characters

Found2characters

Found3characters

Found4characters

Terminal hacked!

Hey there,

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.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


Great first try so far.

Did you check for proper string formatting, especially spaces?
E.g. the instructions tell you to print Found X characters, but your output is Found1characters.

i actually did it right now and changed it to:

console.log(‘Found’+’’+correctGuesses+’’+‘characters’);

still outputs the same error

i now understand what you ment! thank you very much

Welcome there,

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.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.