-
-
Notifications
You must be signed in to change notification settings - Fork 160
NW | 25-ITP-Sep | TzeMing Ho | Sprint 1 | sprint-1-exercises #792
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
ce8105f
cf847a3
13b91a6
efcd3c3
8a77e04
d6a4399
8815813
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,13 @@ | ||
function dedupe() {} | ||
function dedupe(arr) { | ||
// Ensure arr is an array | ||
if (!Array.isArray(arr)) { | ||
throw new Error("Input must be an array"); | ||
} | ||
|
||
// return [] if arr is [] | ||
if (arr.length === 0) return []; | ||
|
||
return [...new Set(arr)]; | ||
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. What's special about |
||
} | ||
|
||
module.exports = dedupe; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,62 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] | |
|
||
// Acceptance Criteria: | ||
|
||
// Making sure input is an array | ||
// When the input is not array, throw an error | ||
test("should return an error when the input is not an array", () => { | ||
const errorMessage = "Input must be an array"; | ||
expect(() => dedupe("not an array")).toThrow(errorMessage); | ||
expect(() => dedupe(123)).toThrow(errorMessage); | ||
expect(() => dedupe({})).toThrow(errorMessage); | ||
expect(() => dedupe(null)).toThrow(errorMessage); | ||
expect(() => dedupe(undefined)).toThrow(errorMessage); | ||
expect(() => dedupe(true)).toThrow(errorMessage); | ||
}); | ||
|
||
// Given an empty array | ||
// When passed to the dedupe function | ||
// Then it should return an empty array | ||
test.todo("given an empty array, it returns an empty array"); | ||
test("given an empty array, it returns an empty array", () => { | ||
expect(dedupe([])).toEqual([]); | ||
}); | ||
|
||
// Given an array with no duplicates | ||
// When passed to the dedupe function | ||
// Then it should return a copy of the original array | ||
test("given an array with no duplicates, it returns the original array", () => { | ||
expect(dedupe(["a", "b", "c"])).toEqual(["a", "b", "c"]); | ||
expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); | ||
expect(dedupe([{ key1: "a" }, { key2: "b" }, { key3: "c" }])).toEqual([ | ||
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. challenge: what would you expect if I did this? dedupe([{ key1: "a" }, { key1: "a" }, { key1: "a" }]) |
||
{ key1: "a" }, | ||
{ key2: "b" }, | ||
{ key3: "c" }, | ||
]); | ||
expect(dedupe([true, false])).toEqual([true, false]); | ||
}); | ||
|
||
// Given an array with strings or numbers | ||
// When passed to the dedupe function | ||
// Then it should remove the duplicate values, preserving the first occurence of each element | ||
// Then it should remove the duplicate values, preserving the first occurrence of each element | ||
test("given an array with strings or numbers, it returns first occurrence of each element", () => { | ||
expect(dedupe(["a", "b", "c", "a", "b", "c", "d"])).toEqual([ | ||
"a", | ||
"b", | ||
"c", | ||
"d", | ||
]); | ||
expect(dedupe([1, 2, 2, 3, 3, 4, 1, 5])).toEqual([1, 2, 3, 4, 5]); | ||
}); | ||
|
||
// Given an array with mixed strings and numbers | ||
// When passed to the dedupe function | ||
// Then it should remove the duplicate values, preserving the first occurrence of each element | ||
test("given an array with mixed strings and numbers, it returns first occurrence of each element", () => { | ||
expect(dedupe([1, 1, 2, "x", "x", "y", "z", 2, 3, 3, "z"])).toEqual([ | ||
1, | ||
2, | ||
"x", | ||
"y", | ||
"z", | ||
3, | ||
]); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
function findMax(elements) { | ||
// ensure elements is an array | ||
if (!Array.isArray(elements)) { | ||
throw new Error("Input must be an array"); | ||
} | ||
|
||
// filter out non-numeric values | ||
elements = elements.filter((element) => typeof element === "number"); | ||
|
||
if (elements.length === 0) { | ||
return -Infinity; | ||
} | ||
if (elements.length === 1) { | ||
return elements[0]; | ||
} | ||
|
||
return Math.max(...elements); | ||
} | ||
|
||
module.exports = findMax; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
function sum(elements) { | ||
// Ensure the input is an array | ||
if (!Array.isArray(elements)) { | ||
throw new Error("Input must be an array"); | ||
} | ||
|
||
// filter out non-numerical values | ||
elements = elements.filter((element) => typeof element === "number"); | ||
|
||
// if the array is empty, return 0 | ||
if (elements.length === 0) { | ||
return 0; | ||
} | ||
|
||
return elements.reduce((acc, num) => acc + num, 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. Could you explain what this reduce function is doing? |
||
} | ||
|
||
module.exports = sum; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
function solution() { | ||
// set the file path | ||
const filePath = path.join(__dirname, "input.txt"); | ||
|
||
// read the file | ||
let frequencies = fs.readFileSync(filePath, "utf-8"); | ||
|
||
// split the numbers by next line and covert them into Number | ||
frequencies = frequencies.split("\n").map((num) => Number(num)); | ||
|
||
// reduce to get the total = 529 | ||
const total = frequencies.reduce((acc, num) => acc + num, 0); | ||
|
||
return total; | ||
} | ||
|
||
console.log(solution()); | ||
|
||
module.exports = solution; |
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.
Do we need a special case here?