-
-
Notifications
You must be signed in to change notification settings - Fork 178
Glasgow | 25-ITP-SEP | Mohammed Abdoon | Sprint 1| Sprint-1 #799
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 5 commits
40539fd
d2e4bd2
3b9b03f
b7e8adf
98abdd3
f2036d3
63bcec9
1851eb6
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) { | ||
| arr = new Set(arr); // Using `new Set()` to store only unique values in the same variable | ||
|
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 use use of Set.
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. Thank you ^^ |
||
| return Array.from(arr); // return the new array using `Array.from()` | ||
| } | ||
|
|
||
| console.log(dedupe(['a','V','a','b','b','c'])); // ['a','b','c'] | ||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| let max = -Infinity; // smallest possible value in JS | ||
| for (const element of elements) { | ||
| // Check if the element is a finite number and greater than current max | ||
| if (Number.isFinite(element) && element > max) { | ||
| max = element; // will always be the greatest so far | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| //console.log(findMax([1.5, 2.3, 0.7, 3.9, 2.8])); // should return 3.9 | ||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,14 @@ | ||
| function sum(elements) { | ||
| let sum = 0; | ||
|
|
||
| for ( const element of elements) { | ||
| if (Number.isFinite(element)) { | ||
| sum += element; | ||
| } | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
|
|
||
| console.log(sum([0.5, 2.5, 3.5])); | ||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,9 @@ | ||
| // 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]; | ||
| if (element === target) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| const includes = (list, target) =>{ | ||
|
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. While this may work in most JS environments, could you double check the for...of syntax and see if ther is a better way to write this.
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. I got it. I didn't declare the variable 'item' properly. I should have put 'let' or 'const' before the variable. It works, but the code fails when running in strict mode ( "use strict"; ). It is safer to declare it the right way. I updated the code so the for..of syntax is better and safer. |
||
| for (item of list) | ||
| if ( item == target ) return true; | ||
| return false; | ||
| } | ||
|
|
||
| module.exports = includes; | ||
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.
Good stuff. You’re currently using Number.isInteger() to check numbers. Could there be a better way to include floating-point numbers in your median calculation too?
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.
Yes, will be better to use Number.isFinite() instead of Number.isInteger if we want to include floating-point numbers.
I updated filter() so floating-point numbers are included.