Q:

Why is for-loop sometimes skipping some iterations?

In the function below, the for loop sometimes skips one iteration.
In some rare cases, I've also found it skipping two iterations.
This sometimes breaks my code and would probably affect my future codes, if my understanding of the for loop remains incomplete.
I further looked into the matter and tried the same with a while loop and found out that this problem doesn't happens if a while loop is used.
Why is the for-loop sometimes skipping some iterations ?
function forLoopString(len)
{
    var string = 'abcdefghijklmnopqrstuvwxyz0123456789';

    var character = '', 
        randomString = '';

    for (var i = 0; i < len; i++) 
    {
        character = string.charAt(Math.floor(Math.random() * string.length-1) + 0); 
        randomString += character;
    }

    if(randomString.length < len)
    {
        console.log('Less than required length!');
        randomString = randomString + '5';
    }
    return randomString;
}

JSFiddle

A:

The loop shown won't "skip" any iterations, but will iterate from [0, len) as told to do.
However, a negative argument to charAt makes it seem like it "skips" because "foo".charAt(-1) == "". The same empty-string result holds for any out-of-bounds to String.charAt:

.. If the index you supply [to charAt] is out of range, JavaScript returns an empty string.

A correction that yields an always-valid index would merely be Math.floor(Math.random() * string.length), without the -1.
Although this is slightly biased (for anyone that really cares) this is 'correct' because Math.random returns a number in the range [0, 1). Thus Math.random() * len returns a value from [0, len); and as an Integer in the same interval after the floor.
Also, it would be good to choose more useful variable names.. and, as Ed points out the +0 is irrelevant because Math.floor returns a (integer) number.

