Skip to content

Commit 7e808a4

Browse files
refactor: combine sum and product calculation into single loop
1 parent 45299ad commit 7e808a4

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,23 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Time Complexity: O(n)
13+
* Space Complexity: O(1)
14+
* Optimal Time Complexity: O(n)
1515
*
1616
* @param {Array<number>} numbers - Numbers to process
1717
* @returns {Object} Object containing running total and product
1818
*/
1919
export function calculateSumAndProduct(numbers) {
2020
let sum = 0;
21-
for (const num of numbers) {
22-
sum += num;
23-
}
24-
2521
let product = 1;
2622
for (const num of numbers) {
23+
sum += num;
2724
product *= num;
2825
}
2926

3027
return {
31-
sum: sum,
32-
product: product,
28+
sum,
29+
product,
3330
};
3431
}

0 commit comments

Comments
 (0)