Skip to content

Commit 82c2fec

Browse files
committed
refactor: clean up formatting and improve JSDoc comments
1 parent c165a71 commit 82c2fec

4 files changed

Lines changed: 65 additions & 53 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
* "product": 30 // 2 * 3 * 5
1010
* }
1111
*
12-
* Time Complexity: O(n) - one loop through the list (the original used two loops, 2n steps, which is also O(n)).
13-
* Space Complexity: O(1) - only two variables (sum, product), regardless of input size
14-
* Optimal Time Complexity: O(n) - every number must be read at least once, so we can't do better than linear time
12+
* Time Complexity: O(n) - one loop through the list (the original used two
13+
* loops, 2n steps, which is also O(n)).
14+
*
15+
* Space Complexity: O(1) - only two variables (sum, product), regardless of
16+
* input size
17+
*
18+
* Optimal Time Complexity: O(n) - every number must be read at least once, so * we can't do better than linear time
1519
*
1620
* Refactor: the complexity can't be reduced, but merging the two loops into
1721
* one makes the code cleaner and does half the steps.
@@ -22,11 +26,12 @@
2226
export function calculateSumAndProduct(numbers) {
2327
let sum = 0;
2428
let product = 1;
29+
2530
for (const num of numbers) {
2631
sum += num;
2732
product *= num;
2833
}
29-
34+
3035
return {
3136
sum: sum,
3237
product: product,
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
/**
2-
* Finds common items between two arrays.
2+
* Finds common items between two arrays.
33
4-
* Inefficiency: the original used secondArray.includes() inside firstArray.filter(), which is a loop inside a loop.
4+
* Inefficiency: the original used secondArray.includes() inside firstArray
5+
filter(), which is a loop inside a loop.
56
6-
* Originally:
7-
* Time Complexity: O(n × m) - filter runs n times and each includes call can scan up to m items (O(n²) if the arrays are the same size).
7+
* Originally:
8+
* Time Complexity: O(n × m) - filter runs n times and each includes call can
9+
* scan up to m items (O(n²) if the arrays are the same size).
810
9-
* Space Complexity: O(n) - the filtered array and the Set hold up to n items.
11+
* Space Complexity: O(n) - the filtered array and the Set hold up to n items.
1012
11-
* Optimal Time Complexity: O(n + m) - every item in both arrays must be read at least once
13+
* Optimal Time Complexity: O(n + m) - every item in both arrays must be read at
14+
least once.
1215
13-
* After refactor (the complexity was reduced from quadratic to linear time by
14-
using a Set for O(1) average lookups instead of includes):
16+
* After refactor (the complexity was reduced from quadratic to linear time by
17+
using a Set for O(1) average lookups instead of includes):
1518
16-
* Time Complexity: O(n + m) - building secondSet is O(m); the loop over firstArray is O(n) with O(1) average has() checks.
19+
* Time Complexity: O(n + m) - building secondSet is O(m); the loop over
20+
firstArray is O(n) with O(1) average has() checks.
21+
22+
* Space Complexity: O(n + m) - secondSet holds up to m items; commonElements
23+
and the final Set hold up to n items.
1724
18-
* Space Complexity: O(n + m) - secondSet holds up to m items; commonElements and the final Set hold up to n items.
1925
* Optimal Time Complexity: O(n + m) - every item in both arrays must be read at least once, so we can't do better than linear
20-
*
21-
* @param {Array} firstArray - First array to compare
22-
* @param {Array} secondArray - Second array to compare
23-
* @returns {Array} Array containing unique common items
24-
*/
26+
*
27+
* @param {Array} firstArray - First array to compare
28+
* @param {Array} secondArray - Second array to compare
29+
* @returns {Array} - Array containing unique common items
30+
*/
2531
export const findCommonItems = (firstArray, secondArray) => {
26-
const secondSet = new Set(secondArray);
27-
let commonElements = [];
32+
const secondSet = new Set(secondArray);
33+
let commonElements = [];
2834

29-
for(let ele of firstArray){
30-
if(secondSet.has(ele)){
31-
commonElements.push(ele);
35+
for (let ele of firstArray) {
36+
if (secondSet.has(ele)) {
37+
commonElements.push(ele);
38+
}
3239
}
33-
}
34-
return [...new Set(commonElements)];
40+
return [...new Set(commonElements)];
3541
};

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
*
77
* Originally:
88
* Time Complexity: O(n²) - for each number, the inner loop scans the
9-
* remaining numbers, so about n * (n - 1) / 2 pair checks.
10-
*
9+
* remaining numbers, so about n * (n - 1) / 2 pair checks.
10+
*
1111
* Space Complexity: O(1) - only the loop counters i and j are used.
12-
*
12+
*
1313
* Optimal Time Complexity: O(n) - every number must be read at least once,
14-
* and a Set gives O(1) average lookups, so one pass is enough
14+
* and a Set gives O(1) average lookups, so one pass is enough
1515
*
1616
* After Refactor (reduced from quadratic to linear time by remembering the
1717
* numbers already seen in a Set instead of re-scanning the list):
18-
*
18+
*
1919
* Time Complexity: O(n) - one loop, with O(1) average has() and add().
20-
*
20+
*
2121
* Space Complexity: O(n) - visitedNumbers can hold up to n numbers.
22-
*
22+
*
2323
* Optimal Time Complexity: O(n) - every number must be read at least once,
24-
* so we can't do better than linear.
24+
* so we can't do better than linear.
2525
*
2626
* @param {Array<number>} numbers - Array of numbers to search through
2727
* @param {number} target - Target sum to find
@@ -40,4 +40,4 @@ export function hasPairWithSum(numbers, target) {
4040
}
4141

4242
return false;
43-
}
43+
}
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
/**
2-
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
3-
*
4-
/**
2+
* Remove duplicate values from a sequence, preserving the order of the first
3+
* occurrence of each value.
4+
*
55
* Originally (nested loops):
66
* Time Complexity: O(n²) - the outer loop runs n times and the inner loop
7-
* scans uniqueItems, which can grow up to n items (about n(n - 1) / 2
8-
* checks in the worst case, when all items are unique)
9-
* Space Complexity: O(n) - uniqueItems can hold up to n items
7+
* scans uniqueItems, which can grow up to n items (about n(n - 1) / 2
8+
* checks in the worst case, when all items are unique).
9+
*
10+
* Space Complexity: O(n) - uniqueItems can hold up to n items.
11+
*
1012
* Optimal Time Complexity: O(n) - every item must be read at least once, and
11-
* a Set gives O(1) average lookups
13+
* a Set gives O(1) average lookups.
1214
1315
* After refactor ([...new Set(inputSequence)]):
1416
* Time Complexity: O(n) - building the Set visits each item once with O(1)
15-
* average inserts; spreading it into an array is also O(n)
16-
* Space Complexity: O(n) - the Set and the result array each hold up to n items
17+
* average inserts; spreading it into an array is also O(n).
18+
*
19+
* Space Complexity: O(n) - the Set and the result array each hold up to n items.
20+
*
1721
* Optimal Time Complexity: O(n) - every item must be read at least once, so we
18-
* can't do better than linear
19-
* /
20-
*
21-
*
22-
* @param {Array} inputSequence - Sequence to remove duplicates from
23-
* @returns {Array} New sequence with duplicates removed
24-
*/
22+
* can't do better than linear.
23+
*
24+
* @param {Array} inputSequence - Sequence to remove duplicates from
25+
* @returns {Array} New sequence with duplicates removed
26+
*/
2527
export function removeDuplicates(inputSequence) {
26-
27-
return [... new Set(inputSequence)];
28+
return [...new Set(inputSequence)];
2829
}

0 commit comments

Comments
 (0)