I want to write a function that removes every second element given that the array is longer than length 2. For instance:
removeEveryOther([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) returns [1, 3, 5, 7, 9]);
removeEveryOther([[1, 2]]) returns [[1, 2]])
Here is my try:
function removeEveryOther(arr){
for (i=0; i < arr.length; i++) {
if (arr.length>2) {
arr.splice((2*i), 1);
}
}
return arr
}
When invoked the function:
removeEveryOther(['1', '2', '3', '4', '5', '6', '7']) returns [ '2', '3', '5', '6' ]
Not sure what I'm doing wrong though, thanks for reading or even helping me out. Have a nice day!