Question #37167

Language is javascript.

Heres the question -

Write a program that loops one thousand times. Add all the even numbers and display the results. Add all the odd numbers and display the results.

Sample output -

Even Total: 249500
Odd Total: 250000

Expert's answer

window.onload = function() {
var loop = 1000,
even_total = 0,
odd_total = 0,
i = 0;

do {
if (i % 2 == 0) {
even_total += i;
} else {
odd_total += i;
}
} while (i++ < loop);

console.log('even total = ' + even_total);
console.log('odd total = ' + odd_total);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS