What is missing in my code?
function numberTally(numList) {
var evenTotal = 0;
var oddTotal = 0;
for (var i = 0; i <= numList.length; i++) {
if i % 2 { // i is odd
oddTotal += numList[i];
// or oddTotal = oddTotal +
numList[i];
}
else { // i is even
evenTotal + numList[i];
}
}
alert(oddTotal);
return evenTotal;
}
Answer / Pankaj Kumar Dagar
In your JavaScript function, you are missing the following lines to calculate the sum of even and odd numbers separately:
```javascript
for (var i = 0; i <= numList.length - 1; i++) {
if (numList[i] % 2 === 0) {
evenTotal += numList[i];
} else {
oddTotal += numList[i];
}
}
alert(evenTotal + ' and ' + oddTotal);
```
| Is This Answer Correct ? | 0 Yes | 0 No |
Explain few difference between null, undefined or undeclared javascript variable?
What is lazy loading in javascript?
how to validate the date(dd/mm/yyyy)using regular expression in javascript? It should also satisfy the leap year feb 29 problem. Please help me.
List few difference between primitive and non primitive javascript data types?
What is the difference between typeof and instanceof operators in Javascript?
Is there an ide for javascript?
What causes memory leaks?
Is javascript harmful?
What is an empty html tag?
What is the data type of variables in javascript?
Why do we need npm?
What is the difference between the operators '==' and '==='?