-
-
Notifications
You must be signed in to change notification settings - Fork 326
London | 26-ITP-May | Vitalii Kmit | Sprint 1 | Exercises #1458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
75011e9
565b186
b6cce2a
bfa78d7
aba332c
057e5a3
ccb0e6d
1221126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,15 @@ | ||
| function dedupe() {} | ||
| function dedupe(list) { | ||
| const seen = {}; | ||
| const result = []; | ||
|
|
||
| for (const item of list) { | ||
| if (seen[item] === undefined) { | ||
| seen[item] = true; | ||
| result.push(item); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
Comment on lines
+1
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good practice but there's a better data structure than simple dict and array. Check out this article here. |
||
|
|
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| let max = -Infinity; | ||
|
|
||
| for (i of elements) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you look up what happens you don't declare "const" in for loop in javascript? Hint : related to "scope" of code blocks |
||
| if (typeof i === "number" && !isNaN(i) && i > max) { | ||
| max = i; | ||
| } | ||
| } | ||
|
|
||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function sum(elements) { | ||
| let sum = 0; | ||
|
|
||
| for (i of elements) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The program can run but why do we need "for (const i of elements)"? |
||
| if (typeof i === "number" && !isNaN(i)) { | ||
| sum += i; | ||
| } | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| // Refactor the implementation of includes to use a for...of loop | ||
|
|
||
| function includes(list, target) { | ||
| for (let index = 0; index < list.length; index++) { | ||
| const element = list[index]; | ||
| // for (let index = 0; index < list.length; index++) { | ||
| for (const element of list) { | ||
|
Comment on lines
+4
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job noticing a better approach for looping! |
||
| if (element === target) { | ||
| return true; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an array has exactly one number (e.g., [5]), what is the median?