Test cases
Test cases | Expected | Result |
---|---|---|
arraySum({5,5,5,5,5}) | 25 | OK |
arraySum({1,3,4,7,8}) | 23 | OK |
arraySum({100,200,300}) | 600 | OK |
Java solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Program8 { public static void main(String[] args) { int a[] = {5,5,5,5,5}; int b[] = {1,3,4,7,8}; int c[] = {100,200,300}; Program8.arraySum(a); Program8.arraySum(b); Program8.arraySum(c); } public static long arraySum(int[] a){ int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum; } } |
Javascript solution:
1 2 3 4 5 6 7 8 9 10 11 |
const arraySum = (array) => { let sum = 0; for (let index = 0; index < array.length; index++) { sum += array[index]; } return sum; } console.log(arraySum([5,5,5,5,5])); //25 console.log(arraySum([1,3,4,7,8])); //23 console.log(arraySum([100,200,300])); //600 |