DEV Community

jolamemushaj
jolamemushaj

Posted on

2 1

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay