#Undefined characters at my pasword generate

3 messages · Page 1 of 1 (latest)

rotund vortex
#

Hi guys, i'm sorry my english is bad.

I need some help about my solo project from scrimba. and I can't figure out where the error is facepalmmichael javascript

low sable
#

Can you please link your scrim ?

stable echo
#

It's because of the +1

The way you wrote your code you will NEVER get an 'A' and you will sometimes try to get a letter beyond the length of the array...

Remember that arrays indexes are 0 - based (the first item is [0]) and so the LAST item is [length - 1]

So for example when an array has 4 items like ['a', 'b', 'c', 'd'] then [0] is 'a' and [3] is 'd' and [4] is undefined - there is no [4].

So, your code does this:

(Math.random() * item.length) + 1

Using our same array of only 4 letters, you can see that the smallest result will be when Math.random() returns 0 thus:

let i1 = Math.floow(Math.random() * item.length) + 1

// when Math.random() returns 0
i1 = Math.floor(0 * 4) + 1;
i1 = Math.floor(0) + 1;
i1 = 0 + 1;
i1 = 1;

// so the smallest letter you'll get is [1] which is 'b'

// similarly when Math.random returns 0.9999999_ (the biggest)
i1 = Math.floor(0.9999999 * 4) + 1
i1 = Math.floor(3.9999999) + 1;
i1 = 3 + 1;
i1 = 4;

// so the largest letter you'll get is [4] which is undefined...  THIS is your problem.

So the solution here is remove the +1 in all those lines!

e.g.

let i1 = Math.floor(Math.random() * item.length);