-
-
Notifications
You must be signed in to change notification settings - Fork 147
Glasgow | ITP May 25 | Mirabelle Morah | Data groups | Sprint 1 #579
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
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,8 @@ | ||
function dedupe() {} | ||
function dedupe(list) { | ||
return [...new Set(list)]; | ||
} | ||
console.log(dedupe(['a','a','b','c','c'])); // returns ['a', 'b', 'c'] | ||
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 of built-in Set feature 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 |
||
console.log(dedupe([5, 1, 1, 2, 3, 2, 5, 8])); // returns [5, 1, 2, 3, 8] | ||
console.log(dedupe([])); // returns [] | ||
|
||
module.exports = dedupe; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
function findMax(elements) { | ||
// Makes a new list called elementFinder that only keeps items from elements | ||
// that are real numbers (not words, not NaN) | ||
const elementFinder = elements.filter( | ||
(el) => typeof el === "number" && !isNaN(el) | ||
); | ||
|
||
// If there are no numbers in the list, return -Infinity (which means “no biggest number”). | ||
if (elementFinder.length === 0) { | ||
return -Infinity; | ||
} | ||
|
||
// Otherwise, return the biggest number from elementFinder. | ||
return Math.max(...elementFinder); | ||
} | ||
|
||
console.log(findMax([1, 2, 20, 4, 5])); | ||
console.log(findMax([1, "2", 3, "four", 5])); | ||
console.log(findMax([NaN, null, 5, 10, "abc"])); | ||
console.log(findMax([NaN, null, "5", "10", "abc"])); | ||
|
||
module.exports = findMax; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,21 @@ | ||
function sum(elements) { | ||
function sum(list) { | ||
// Remember this filters the list to only include numbers and ignore strings or other types | ||
const filterList = list.filter( | ||
(el) => typeof el === "number" && !isNaN(el) | ||
); | ||
|
||
// If there are no numbers, return 0 | ||
if (filterList.length === 0) { | ||
return 0; | ||
} | ||
// If the sum is negative, return 0; otherwise, return the sum | ||
const total = filterList.reduce((acc, curr) => acc + curr, 0); | ||
return total; | ||
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 of reduce function |
||
} | ||
|
||
console.log(sum([10, 20, 30])); // 60 | ||
console.log(sum(['hey', 10, 'hi', 60, 10])); // 80 | ||
console.log(sum([])); // 0 | ||
console.log(sum([-10, -20, 30])); // 0 | ||
|
||
module.exports = sum; |
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 solution