Repository navigation
Conversation
| // The refactor code .... | ||
| export function calculateSumAndProduct(numbers) { | ||
| let sum = 0; | ||
| let product =1; |
| } | ||
|
|
||
|
|
||
| // This refactor code is better because it avoid to check every single item from two arrays for filter items & searching that each items includes in the second array . |
There was a problem hiding this comment.
Your complexity analysis is correct if the length of both arrays are
Also, your explanation does not quite explain how you derived the optimal complexity. Could you elaborate?
| export function hasPairWithSum(numbers,target){ | ||
| const seen = new Set(); | ||
|
|
||
| for (const num of numbers) | ||
| { | ||
| const needed = target-num; | ||
| if (seen.has(needed)) | ||
| { | ||
| return true; | ||
| } | ||
| else | ||
| { | ||
| seen.add(num); | ||
| } | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
-
Why not use VS Code's "Format document" feature or any other formatting tool to keep the code consistently formatted?
-
The refactored code has a bug. Please fix it.
| } | ||
|
|
||
|
|
||
| // The refactor code is better because it avoid nested loop but it create set so space complexity will be o(n). No newline at end of file |
There was a problem hiding this comment.
Saying "avoid nested loops" does not fully explain why the refactored implementation is better than the original. Instead, you could compare their time complexities to clearly explain the performance improvement.
On separate note, could you elaborate how you determined the complexity of the refactored implementation is
| } | ||
|
|
||
|
|
||
| //The refactor code is better because it avoid the nested for loop operation .... |
There was a problem hiding this comment.
Could you also rephrase this statement to explain how you determine the complexity of the refactored code is
Learners, PR Template
Self checklist
Task code
CYF-1173
Changelist
I have chosen JavaScript as the programming language and refactored the code and commented in each file why the refactored code is better.
Please check and provide feedback ..
Thanks ..
AYK