I'm troubleshooting this JavaScript code snippet:
const evenLast = arr => {
return arr.filter((a,idx) => {
if(!(idx&1)) return (a * arr[arr.length-1])
})
}
console.log(evenLast([2, 3, 4, 5]))
When I run the code, it returns [2,4]
instead of [10, 20]
. I'm curious why the condition
if(!(idx&1)) return (a * arr[arr.length-1])
is resulting in only (a)
rather than multiplying by last_item_of_array
.