Build a Password Generator App - Build a Password Generator App

Tell us what’s happening:

I think i have made this code way more complex than it needs to be. However i am returning a string and its stating that it is not a string. I checked the typeof before compleating and its still stating it snot a string. Can i get some help, will i be best restarting with the process being entirely via string rather than starting with array.

Your code so far


function generatePassword(passwordLength){
  const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  const symbols = "!@#$%^&*()"
  let password = [];

  if(passwordLength < 6){
    console.log(`Password length must be grater than 5`);
    return
  }

  //getting 2 unique symbles from start of password length
  for(let j = 0; j < 2; j++){
    let uniqueChar = Math.floor(Math.random() * symbols.length);
    let char = symbols[uniqueChar];
    password.push(char);
  }

  //adding in random letters and numbers for rest of password length
  for(let i = 0; i < passwordLength - 2; i++){
    let randomIndex = Math.floor(Math.random() * letters.length);
    let char = letters[randomIndex];
    password.push(char) ;
  }

  //shuffle the result of password
  for(let i = 0; i < password.length; i++){
    let random = Math.floor(Math.random() * password.length)
    let temp = password[i];
    password[i] = password[random];
    password[random] = temp;
  }

  //convert to sting and return
  password = password.join("");
  console.log(typeof password);
  return password;
}

let password = generatePassword(7);
console.log(`Generated password: ${password}`);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Build a Password Generator App - Build a Password Generator App

When the password length is less than 6, your function returns nothing (undefined), but it should always return a string - so you need to return an empty string instead in that case.

Hi there!

Were you asked to do any of the following?

And did you do this?

You should use the following string and different Math methods to help you return a new string with random characters in it: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()

Happy coding!

Oh, I thought…
5. Your function should return a randomly generated password which contains more than one unique character.

Was stating it needed more than one (so minimum 2) of the unique characters. I thought that meant 2 of the symbals as they are unique characters.

I wanted to do the shuffle to see how it turned out.

Ive changed the approach completely and just done a pure string code due to the array one continuously stating its not a string.

Here is the new one.

let password = "";

function generatePassword(passwordLength){
  const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
  

  for(let i = 0; i < passwordLength; i++){
    let random = Math.floor(Math.random() * characters.length - 1);
    let passChar = characters[random];
    password += passChar;
  }
  console.log(`Generated password: ${password}`);
  return password;
}

generatePassword(5)

I am getting the correct result ive also checked with **${password}** to make sure its the correct length. Ive also tried with just characters.length);
So tried without the - 1
I just cannot see why its failing what seems to be getting done. Ive created the final string added the value to password and returned password that is a string.

Hi @toothpick164 ,

Please reread User story #3 and #4. The global password variable should be assigned to a function call.

This console.log() should be outside of the function to show the result of calling your function.

You are also using a password variable inside your function. It’s usually not a good idea to have a function rely on a global variable. I suggest using another variable name.

Happy coding!

Got it thank you, sorted the password variable and were it was placed in the code.
called the function from it and sorted a result variable inside the loop to help it run correctly.

Thanks for the help. Not entirely sure why i was getting the return a string and it being one but failing though. Was it due to the way the value was being created and concatenated into the variable?
For the initial code sent specifically.

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