After successfully segregating the odd and even numbers, I am faced with a challenge in determining how to add the odds together and the evens together before subtracting them to find the final result. For example:
(1 + 3 + 5 + 7 + 9) - (2 + 4 + 6 + 8) = 5
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sumDiff(numbers);
function sumDiff(numbers) {
let even = [];
let odd = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 === 0) {
even.push(numbers[i]);
} // end
else {
odd.push(numbers[i]);
}// end else
} //end of for loop
console.log(odd);
console.log(even);
} // end of function
While I don't need the complete solution at this point, I do require some direction on how to proceed. It seems logical to separate the odd and even numbers first, but should I create a new function or can this be accomplished within the existing one?