How do I extract even elements of an Array?

Either use modulus:

for (var i = 0; i < a.length; i++) {
    if(i % 2 === 0) { // index is even
        ar.push(a[i]);
    }
}

or skip every second element by incrementing i accordingly:

for(var i = 0; i < a.length; i += 2) {  // take every second element
    ar.push(a[i]);
}

Notice: Your code actually takes the elements with odd indexes from the array. If this is what you want you have to use i % 2 === 1 or start the loop with var i = 1 respectively.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)