-
-
Notifications
You must be signed in to change notification settings - Fork 220
London | 25-ITP-Sep | Adnaan Abo | Sprint 1 | Sprint-1 #791
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
Changes from all commits
60910f6
c03fec7
d6ed86a
9009a30
b31b8fa
e8d38cd
779e97e
b3df5e1
1a1c5c7
70b6bc3
bb9d230
cbe2b25
df25099
ebce7b3
bd3bce9
d01403b
5c53126
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,7 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| // Create a new Set from the array to remove duplicates | ||
| // and then convert it back to an array. | ||
| return Array.from(new Set(arr)); | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,24 @@ | ||
| function findMax(elements) { | ||
| if (elements.length === 0) { | ||
|
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. You may have heard of the DRY (Don't Repeat Yourself) principle in coding, which emphasises reducing the amount of code that is repeated (such as the same
Author
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. Removed the first if statement since since the if statement at line 8 covered both cases. and i have updated the code accordingly |
||
| return -Infinity; // Return -infinity for an empty array | ||
| } | ||
|
|
||
| // If non-numeric elements are present, we will ignore them | ||
| elements = elements.filter(element => typeof element === 'number'); | ||
| if (elements.length === 0) { | ||
| return -Infinity; // if all elements are non-numeric, return -Infinity | ||
| } | ||
|
|
||
| // Start with the first numeric element as the maximum | ||
| let max = elements[0]; // Assume the first element is the maximum | ||
|
|
||
| for (let i = 1; i < elements.length; i++) { | ||
| if (elements[i] > max) { | ||
| max = elements[i]; // Update max if a larger element is found | ||
| } | ||
| } | ||
|
|
||
| return max; // Return the maximum value found | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| function sum(elements) { | ||
| return elements | ||
| .filter((el) => typeof el === "number") | ||
| .reduce((acc, curr) => acc + curr, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
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.
This solution is already perfect, but just as an extension, could you show me how you could express this if...else statement using a ternary operator?
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.
Your right we can express the if...else statement using a ternary operator here is how our median calculation will look like using the ternary operator:
numericList.sort((a, b) => a - b);
const middleIndex = Math.floor(numericList.length / 2);
const median = numericList.length % 2 === 0
? (numericList[middleIndex - 1] + numericList[middleIndex]) / 2
: numericList[middleIndex];
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.
Exactly! Great stuff. In my experience with javascript/typescript, the ternary operator is often preferred over if..else, if there are no "else if" conditions