Need help with a challenge please!

Hi all ,

I am stuck looking for a solution for a challenge for a bootcamp - I am a n00b, so I cannot understand where am I getting this wrong. Could someone please help me identify what’s wrong?

I have been googling while loops and if statements, and if statements withing while loops, to verify if I made a mistake - which I did, clearly - but where, exactly?

Here is the challenge:

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:

  1. 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).

  2. 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.

  3. 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!

Here’s my code:

var correctGuesses = 0;
var password = 4;
var tries;

while (correctGuesses < 4){
    tries = Math.ceil(Math.random()*3);
    
    if(tries === 1){
        correctGuesses++;
        console.log('Found' + ' ' + correctGuesses + ' ' + 'characters');
    } else if (tries === 2){
        correctGuesses = 0;
        console.log('Starting over');

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

I am a noob and am also stuck in this for a while now (pun intended!).

The error that it gives me is:

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

Thank you for your help @camperextraordinaire :slight_smile:

You might just have an extra space in the first console log.

Hi @lasjorg ,

Thank you so much for your feedback - good catch!
I have corrected it, both here and in the IDE, but unfortunately the error is still present. :frowning:

Hi all!,

Found the solution. I kept reading the MDN web docs regarding Loops and iterations, and found what was missing!

Instead of

} else if (tries === 3){
       console.log(correctGuesses);
    }

it actually tells me:
3. You guessed incorrectly, but have not been detected yet, correctGuesses is kept with the same value.

It doesn’t TELL ME to console.log correctGuess, it just tells me that I have not been detected - translated: keep guessing.

According to MDN web docs:

The continue statement can be used to restart a while, do-while, for, or label statement.

  • When you use continue without a label, it terminates the current iteration of the innermost enclosing while, do-while, or for statement and continues execution of the loop with the next iteration. In contrast to the break statement, continue does not terminate the execution of the loop entirely. In a while loop, it jumps back to the condition. In a for loop, it jumps to the increment-expression.

Check the full article here: Loops and iterations, while statement

So I tried this:

    } else if (tries === 3){
       continue;
}

And it worked! wohooo!

Thank you so much for your help :slight_smile:

That was going to be my next guess as well. Good job figuring it out yourself.

Considering the condition isn’t doing anything, I would imagine you don’t need it at all. Or does the test expect it for some reason?

Hi @lasjorg,

Thank you so much for your input :slight_smile:

Indeed, it also works without the block of code

      } else if (tries === 3){
       continue;
}

So even if we remove it , it will run correctly.

What a tricky exercise!

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