Ask Question
13 November, 01:13

Write a method that take an integer array as a parameter and returns the sum of positive odd numbers and sum of positive even numbers.

+5
Answers (1)
  1. 13 November, 01:29
    0
    I will code in jа vascript:

    function sumOddAndEven () {

    //Define and initialize variables.

    var numbers = [1,2,3,4,5,6,7,8];

    var sum = [0,0];

    for (var i = 0; i < numbers. length; i++) { / / loop to go throght the numbers

    if (numbers[i] > 0) { / / if the number is positive

    if (numbers[i]%2 = = 0) { / / if number is even and

    sum[0] = sum[0] + numbers[i]; / /adds in the first place of sum

    }

    else{

    sum[1] = sum[1] + numbers[i]; / / else adds in the second place of sum

    }

    }

    }

    return sum; / /return an array with the sum of the positive evens numbers in the first place and the sum of the positive odd numbers in the second place.

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a method that take an integer array as a parameter and returns the sum of positive odd numbers and sum of positive even numbers. ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers