I'm trying to solve a problem on a testing website and I'm getting confused on trying to find the odd numbers from a passed in array at odd index.
Code I have so far;
function codeNation(arr) {
let newArr = [];
arr.forEach(function(value) {
if (value % 2 !== 0) {
newArr.push(value);
}
});
return newArr;
}
console.log(codeNation([1, 3, 5, 7, 9, 11]));
In the above example I want it to return [3, 7, 11] but I can't figure out how. Can you please fix my code and explain to me the best way of getting an odd number at an odd index from a passed in array to the function? The order of the numbers has to be retained.
forand try to implement it. This works for me.const newArr = []; for (let i =1; i<arr.length; i+=2 ) { arr[i] %2 !== 0 && (newArr.push(arr[i]))}