-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraySum.js
More file actions
43 lines (29 loc) · 983 Bytes
/
arraySum.js
File metadata and controls
43 lines (29 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Initialize the array with the given numbers
var numbers = [10, 25, 25, 94, 78, 55, 94, 45];
// Initialize the sum variable
var sum = 0;
// Use a for loop to iterate through the array and calculate the sum
for (var i = 0; i < numbers.length; i++) {
var element = numbers[i];
sum = sum + element;
// sum += numbers[i];
}
// Output the result
console.log("The sum of the numbers is: ", sum);
// Define the function to sum the numbers in an array
function sumArray(arr) {
// Initialize the sum variable
var sum = 0;
// Use a for loop to iterate through the array and calculate the sum
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
// Return the result
return sum;
}
// Initialize the array with the given numbers
var numbers = [10, 25, 25, 94, 78, 55, 94, 45];
// Call the function and store the result
var totalSum = sumArray(numbers);
// Output the result
console.log("The sum of the numbers is: ", totalSum);