Guess the pass - playing around with loops

Hi community,

I just got my Codecademy pro membership several days ago to start learning how to code.
Besides projects the course is offering me, I also try to learn by having my personal side-projects.

One of my projects is making a password guesser. A great way to learn about the loops.
I managed to make it work but especially longer passwords (maxLenght>7) will not be guessed by my code.

Anyone can provide me with (easy to understand) feedback on how to improve my code in order to make it work for passwords from let’s say a length of 10?

Here is my code:
(sorry for leaving some personal work-comments in between ;))

const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const password = 'guessit';

// Now let's try to make a guesser that works for passwords of a length up to the maxLength
//still works okay if the password has a length of six charachter, but seven seems to be too much!
let maxLength = 7;
let guess = '';
let j = 0;

// Creating a timestamp
var timestamp =;
// Converting it back to human-readable date and time
var d = new Date(timestamp);

const guessInf = () => {
do {
    let pickLetter = alphabet[Math.floor(Math.random()*(alphabet.length))];
    guess = guess + pickLetter;
    if (guess === password) {
        console.log(`Congratulations, the password was ${guess}.\nThe number of guesses needed was: ${j}.` )
    } else if (guess.length > maxLength) {
            guess = '';
    } while (guess !== password);

var timestamp =;
var d = new Date(timestamp);

What you currently do is known as brute force approach, which becomes less effective with longer password

you could be more systematic then completely random, for example you could do:

const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

const alphabetLength = alphabet.length;
const guess = [];

let maxLength = 7;

// gives array with 7 times letter `a`
for (let i = 0; i < maxLength; i++){


and then slowly increase one letter at a time. So you increase the first element of guess array to b, then c and so forth (you could use loop and modulo operator)

Thanks for your reply. I think I do understand your concept (but not how your code would truly help me out there haha).

You mean, first let it check all words starting with alphabet[0], then go over to all possibilities with alphabet[1] etc.?

yea, that is the challenge :wink: That is why I shared the concept, because I think figuring out the code is a good challenge for you

i was thinking using aaaaaaa for your first guess, then baaaaaa for your second guess, caaaaaa for the third guess and so forth. once you had zaaaaaa you move on to abaaaaa and so forth. This is where the loops and modulo operator comes in

hope I got the amount of a's right, but you get the idea.

1 Like