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 = Date.now();
// Converting it back to human-readable date and time
var d = new Date(timestamp);
console.log(d);
const guessInf = () => {
do {
let pickLetter = alphabet[Math.floor(Math.random()*(alphabet.length))];
guess = guess + pickLetter;
j++;
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);
};
guessInf();
var timestamp = Date.now();
var d = new Date(timestamp);
console.log(d);