DEV Community

jolamemushaj
jolamemushaj

Posted on

Removing elements from an array

Take an array and remove every second element from the array. Always keep the first element and start removing with the next element.
Example:
Given the array --> [1, 2, 3, 4, 5]
The result will be --> [1, 3, 5]

function removeEl(arr) {
    const newArr = [];
    for (let i = 0; i < arr.length; i = i + 2) {
        newArr.push(arr[i]);
    }
    return newArr;
}

console.log(removeEl([1, 2, 3, 4, 5]));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)