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)

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈