Skip to content
Advertisement

Displaying a concat array in an HTML element

I have multiple arrays that have been combined depending on user response. The only problem I have at this point is getting this final array to show up in the text field for the end user to see. Thanks for all the potential help!

JavaScript

I have narrowed the problem to either the write function or the generate function. The booleans and math.random functionality are working as expected and have confirmed this in the console.

EDIT: Adding HTML for those who have asked. As well as deleting the redundant function that was the same as the write password function.

JavaScript

Advertisement

Answer

Several issues:

  • enterpassword is a function that is never used (and has a spelling issue and missing parentheses). It looks to have the same purpose as writePassword, so it can be removed.
  • passwordLength is going to be a string, better convert it immediately to a number data type, or else code will continue with some invalid input like “one”
  • The expression for getting a random character is wrong. It should have chosenPassword.length instead of chosenPassword.
  • The algorithm does not ensure that at least one character from each selected character group is taken. It only makes sure no other character is taken.

Some other remarks:

  • Using prompt is not very user-friendly, as user is forced to follow that input path and cannot go back to change a previous answer. Use input elements instead (number and checkbox types), so that when user clicks the button, all input is already there.

  • Avoid code repetition, and deal with each character group using the same code in a loop.

To ensure at least one character is taken from each selected character group, you could take a random character from each group, then append random characters from any group (like you did, or by shuffling & slicing), and then shuffle to get those first characters at random places too.

Here is your code adapted to work like that:

JavaScript
JavaScript

With prompt and confirm

Not ideal, but if you really have to do this with prompt and confirm, then you can still reuse the logic of the above code, but change the input method:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement