Thursday, 25 May 2017

How to write a function that returns the elements on odd positions in an array

I can get it to return the odd index of number if I just run a for loop but when I'm trying to turn it into a function it't not returning anything?

    function oddArrayIndex( arrayName, emptyIndex ) {
    for (var i = 1; i < arrayName.length; i += 2) {
        emptyIndex.push(arrayName[i]);
        }
    }
    var arr = [ 1, 2, 3, 4, 5, 6, 7];
    var oddIndex = [];
    oddArrayIndex(arr, oddIndex);



via Jerry Hoglen

No comments:

Post a Comment