Question #54759

Create a script called odd_even.js. Write a program that generates 100 random numbers and keeps count of how many of those random numbers are even and how many of them are odd. Sum the even numbers and sum the odd numbers. Use a function to check if a given number is even or odd and use a function that returns the sum of a given array. Display the results as:
Even Number Count: 72
Odd Number Count: 28
Sum Even: 560
Sum Odd: 433
1

Expert's answer

2015-09-19T00:00:43-0400

Answer on Question #54759 - Programming - AJAX | JavaScript | HTML | PHP

Contents of odd_even.js file:


var min = 1,
max = 100,
odd = [], even = [], oddCount = 0, evenCount = 0;
// returns is number odd or even
function isOdd(n) {
if (n % 2 != 0) {
return true;
} else {
return false;
}
}
// returns sum of array elements
function arraySum(arr) {
var sum = 0;
for (var i=0; i<arr.length; i++) {
sum += arr[i];
}
return sum;
}
for (var i=0; i<=100; i++) {
var number = parseInt(Math.floor(Math.random() * max) + min); // get random number
if (isOdd(number)) {
odd.push(number);
oddCount++;
} else {
even.push(number);
evenCount++;
}
}
var result = 'Even Number Count: ' + evenCount.toString() + "\r\n"
+ 'Odd Number Count: ' + oddCount.toString() + "\r\n"
+ 'Sum Even: ' + arraySum(even) + "\r\n"
+ 'Sum Odd: ' + arraySum(odd) + "\r\n";


console.log(result); // show result into console

http://www.AssignmentExpert.com/

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!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS