London | 26-ITP-May | Vitalii Kmit | Sprint 1 | Exercises - #1458
London | 26-ITP-May | Vitalii Kmit | Sprint 1 | Exercises#1458Vitalii-code wants to merge 11 commits into
Conversation
Khantdotcom
left a comment
There was a problem hiding this comment.
- Fix median logic
- Look up what if you don't declare "const" in "for loop"
- Improved data structure of dedupe(list)
| list = list.filter((item) => typeof item === "number"); | ||
| list.sort((a, b) => a - b); | ||
|
|
||
| if (list.length <= 1) { |
There was a problem hiding this comment.
If an array has exactly one number (e.g., [5]), what is the median?
There was a problem hiding this comment.
It returns null because of the length check. But I think It should return 5 in case of passing [5]
| // for (let index = 0; index < list.length; index++) { | ||
| for (const element of list) { |
There was a problem hiding this comment.
Good job noticing a better approach for looping!
| function dedupe(list) { | ||
| const seen = {}; | ||
| const result = []; | ||
|
|
||
| for (const item of list) { | ||
| if (seen[item] === undefined) { | ||
| seen[item] = true; | ||
| result.push(item); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
There was a problem hiding this comment.
Good practice but there's a better data structure than simple dict and array. Check out this article here.
There was a problem hiding this comment.
Yeah using sets here makes more sense
| function findMax(elements) { | ||
| let max = -Infinity; | ||
|
|
||
| for (i of elements) { |
There was a problem hiding this comment.
Can you look up what happens you don't declare "const" in for loop in javascript? Hint : related to "scope" of code blocks
There was a problem hiding this comment.
Yeah it can update a variable called i outside of the scope which can cause issues
| function sum(elements) { | ||
| let sum = 0; | ||
|
|
||
| for (i of elements) { |
There was a problem hiding this comment.
The program can run but why do we need "for (const i of elements)"?
Learners, PR Template
Self checklist
Changelist
Work on sprint one exercises