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 Posted / 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 |
Post New Answer View All Answers
Write a main method that prompts the user to enter the starting population, birth and death rates, and the number of years. The input birth and death rates cannot be negative, the starting population cannot be less than 2, and the number of years greater than 0. If the user enters an invalid value, this program will prompt user to re-enter the value until a valid value has been read. When all input values are valid, call on the methods above to compute estimated population and print result.
Is it possible to do 301 redirects in javascript ?
Program to return the address family of a socket
How to setting a cookie with the contents of a textbox?
How to convert a string to a number using javascript?
How do you know if a set of points is a function?
Define escape() function?
how to get no of items from from weblist?