I'm creating a small game and I have a peace of code that collects the pressed keys during an interval of time.
var pressedKeys = [];
setTimeout(function() {
for(var i = 0; i < pressedKeys.length; i++)
alert("Time is up you have inputed " + pressedKeys[i] + " length " + pressedKeys.length);
}, 3000);
$(document).keydown(function(evt) {
var key = evt.keyCode;
if (pressedKeys.length < 1) {
pressedKeys[0] = key;
} else {
pressedKeys[pressedKeys.length + 1] = key;
}
});
I'm new to javascript and I can't understand why I have unidentified values in the array. The funny thing to me is if I do the loop with a foreach I do not get the unidentified values.
Can some one please explain this to me. I would be very thankful.