#(SOLVED)How to stop this function at 15 characters

7 messages · Page 1 of 1 (latest)

dull sparrow
#

Hi guys. I have a problem, when i hit the button on my page its supposed to generate a 15 character password from an Array.

This currently works but i need to make sure it cant exceed 15 characters.

Right now you can keep pushing the button and it will keep Adding another 15 characters (so 15 to 30 to 45 and so forth)

I'm almost certain the solution is either Boolean Statements.
Or a mistake with the "+=" operator in the bottom.

thank you in advance,
I hope my question makes sense 😄

function generatePasswords() {
    for (let i = 0; i < 15; i++) {
            let randomIndexOne = Math.floor( Math.random() *       characters.length )
            let randomIndexTwo = Math.floor( Math.random() *        characters.length )

            passwordBoxOne.textContent += characters[randomIndexOne]
            passwordBoxTwo.textContent += characters[randomIndexTwo]
    }
}
ebon cave
#

+= is going to keep on adding characters to whatever you have in there, so if there are 15 characters already, it will end up adding another 15 on top of them, and so on. The solution is to "clear out" your passwords' text contents at the beginning of each invocation of the generatePasswords function. I recorded a scrim for someone else who encountered the same issue; let me see if I can find it...

#

(Their approach was slightly different than yours, but the same basic ideas apply)

dull sparrow
#

How to stop this function at 15 characters(SOLVED)

#

(SOLVED)How to stop this function at 15 characters