I'm a beginner in JavaScript and doing an online test to improve my skills. I came across to this question:
- Add 1 point for each even number in the array
- Add 3 points for each odd number in the array
- Add 5 points every time the number 5 appears in the array
So if I am given this array as an example: ([1,2,3,4,5]) the output should be 13
This is what I have got so far:
export function find_total( my_numbers ) {
for(let i = 0; i < my_numbers.length; i++){
if(my_numbers[i] % 2 === 0) {
total = total + 1
}
if(my_numbers[i] % 2 === 1) {
total = total + 3
}
if(my_numbers[i] === 5) {
total = total + 5
}
return total
}
console.log(total)
}
But it is giving me errors. I get the logic in English but couldn't put it in JS. What would be the right syntax here?