- 4
Password Generator
Okay, ye so i tried my best but it has no output so if anyone could adjust or fix my code that would be nice because im not to sure what to do next https://code.sololearn.com/W66k2u5OX0dt/?ref=app
20 Answers
+ 2
To pick a random letter from a list:
const letters = "abcde..";
const randomLetter = letters[Math.floor(Math.random() * letters.length)];
console.log(randomLetter);
`Math.random()` gives you a random number between 0 and 1. Multiplying that by `letters.length` will give you a random number between 0 and however many letters there are.
`Math.floor` will round it down to the nearest whole number. That number you can then use to index into the list, using square brackets.
Hope that helps. To generate a password, simply do that 8 times, and add it all up.
+ 2
Well I don't just want to spell it all out, that's no way to learn :p
Look at my first comment, then my latest. Put the two together..
+ 1
Use Math.round() instead of Math.floor()(it drops the floor to integer)
Because Math.round() will round it to nearest whole number(it can increment or decrement some value from it to make it a whole number)
While Math.floor() will just drop the value after points(it will always decrement a value to make a whole number)
By using Math.floor chances will be very low of getting the word z
As
Math.floor(25.9999999) = 25
Math.round(25.6) = 26
+ 1
Oh thats why. Btw ty for correction
0
UMAR FAROOQ That will lead to errors!
If you have a list of size 26, and want to pick a random element from it, you need to generate a random number between 0 and 25.
letters[25] == "z"
So Math.floor is absolutely correct in this case.
0
Schindlabua Can i like add a variable like var length = 8 and make that equal thenpasswrod somehow?
0
Your Mom sure.
let password = ""; // fill that up in the loop
const length = 8;
for(let i = 0; i < length; i++) {
// generate a random letter here, then
password += randomLetter;
}
// finished password is now in variable `password`
0
Im confused where did the randomCharacter come from? If you code show me the code that my explain things a bit better,thanks
0
Okay well i think i figured it out but still didnt work
https://code.sololearn.com/W66k2u5OX0dt/?ref=app
0
Same thing you did but using document.write 😎
- 1
Nevermind im being an idiot
- 1
Okay, well it works now but its kind of odd i mean, For example if i pressed run it might come out as Ao1234567 and if i pressed run again it will still keep the numbers there: O_1234567 Schindlabua
- 1
I want it to vary and i want the letters to move around more if that makes sense
- 1
Use document.write
- 1
That doesnt change anything Ahmed Waiz
- 1
cause and effect: You see 01234567 in your output. What line could be causing it? Try removing lines of code until the 01234567 disappers. Then think about what would need to happen to make some different output appear.
I've given you the keys to the kingdom, you just need to copy and paste the two pieces of code I've given you, no extra coding required :p
Also, you might want to review the javascript lessons on loops, and strings.
- 1
No no, im not rlly good at explainging things. The code is fine, it’s kind of ‘repetitive’ in a way i guess. If i clivk the run button itll come out as Ao1234567. I want it to come put as something line Ao37<%j0. Or where the letters vary. You can try my code and see what i mean
- 1
Answer? Or r u working on it
- 1
- 2
Umm ok, but the answer to my question is what i want Ahmed Waiz