beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Array Range Sum

Geoffrey Challen // 2022.1.0

Write a method named arrayRangeSum. It receives an IntArray and a positive Int range as parameters. You should return the sum of all the elements in the array that are between range and range * -1, non-inclusive.

For example, given the array {1, -4, 2, 24, -124} and the range 8, you would return -1: 1 + -4 + 2, since these are the values in the array strictly greater than -8 and strictly less than 8. Given the range 124, you should return 23: 1 + -4 + 2 + 24, since these are the values in the array strictly greater than -124 and less than 124.

Note that this problem is a great fit for the for in loop!