Skip to content

Commit b2a67d4

Browse files
committed
refactor code for calculateSum and product
1 parent 134e683 commit b2a67d4

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

‎Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity:
13-
* Space Complexity:
14-
* Optimal Time Complexity:
12+
* Time Complexity:O(2n) originall have this as using 2 separate loops
13+
* Space Complexity:O(1) no extra space used that rows with input
14+
* Optimal Time Complexity:O(n) must at least visit each number once
1515
*
1616
* @param {Array<number>} numbers - Numbers to process
1717
* @returns {Object} Object containing running total and product
1818
*/
19+
20+
// here we are using 2 loops one for sum and other for product
21+
// but we can do it only in one loop so code will be more simple and faster
22+
1923
export function calculateSumAndProduct(numbers) {
2024
let sum = 0;
21-
for (const num of numbers) {
22-
sum += num;
23-
}
24-
2525
let product = 1;
2626
for (const num of numbers) {
27+
sum += num;
2728
product *= num;
2829
}
29-
3030
return {
3131
sum: sum,
3232
product: product,

0 commit comments

Comments
 (0)