-
-
Notifications
You must be signed in to change notification settings - Fork 278
London | ITP-Jan-26 | Mohsen Zamani | Sprint 1 | Coursework #949
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 2 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,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(elements) { | ||
| return [...new Set(elements)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,32 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] | |
| // 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"); | ||
| describe("dedupe", () => { | ||
| it("returns an empty array if passed 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 | ||
| [ | ||
| { input: [1, 2, 3], expected: [1, 2, 3] }, | ||
| { | ||
| input: [4542543, 65756756, 433254], | ||
| expected: [4542543, 65756756, 433254], | ||
| }, | ||
| ].forEach(({ input, expected }) => | ||
| it( | ||
| "returns a copy of original array when passed array with no duplicates ", | ||
| () => expect(dedupe(input)).toEqual(expected), | ||
| expect(dedupe(input)).not.toBe(expected) | ||
|
||
| ) | ||
| ); | ||
|
|
||
| // 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 | ||
| [ | ||
| { input: [1, 2, 3, "apple", 3, 2, {}], expected: [1, 2, 3, "apple", {}] }, | ||
| { | ||
| input: [4542543, undefined, 4542543, 4542543, null, [], null], | ||
| expected: [4542543, undefined, null, []], | ||
| }, | ||
| ].forEach(({ input, expected }) => | ||
| it("returns array wit unique values in their first occurrence index when passed an array with numbers and non-number values ", () => | ||
| expect(dedupe(input)).toEqual(expected)) | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| function findMax(elements) { | ||
| const numberElements = elements.filter( | ||
| (el) => typeof el === "number" && !Number.isNaN(el) | ||
| ); | ||
| if (numberElements.length === 0) return -Infinity; | ||
| return Math.max(...numberElements); | ||
| } | ||
|
|
||
| // console.log(findMax(findMax([0, null, 1]))); | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) return 0; | ||
| return elements.reduce((acc, curr) => { | ||
| return Number.isFinite(curr) ? acc + curr : acc; | ||
| }, 0); | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,27 +10,84 @@ const sum = require("./sum.js"); | |
|
|
||
| // Acceptance Criteria: | ||
|
|
||
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
|
|
||
| // Given an array containing non-number values | ||
| // When passed to the sum function | ||
| // Then it should ignore the non-numerical values and return the sum of the numerical elements | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the sum function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| describe("sum()", () => { | ||
| it("returns 0 for empty array", () => expect(sum([])).toBeCloseTo(0)); | ||
|
|
||
| [ | ||
| { input: [4], expected: 4 }, | ||
| { input: [367], expected: 367 }, | ||
| { input: [7958463], expected: 7958463 }, | ||
| ].forEach(({ input, expected }) => { | ||
| it(`returns the sum for arrays with one number`, () => | ||
| expect(sum(input)).toBeCloseTo(expected)); | ||
| }); | ||
|
|
||
| [ | ||
| { input: [-9], expected: -9 }, | ||
| { input: [-367, -5], expected: -372 }, | ||
| { input: [-7958463, -100, -202, -6453], expected: -7965218 }, | ||
| ].forEach(({ input, expected }) => | ||
| it("returns the correct sum for array with only negative values", () => | ||
| expect(sum(input)).toBeCloseTo(expected)) | ||
|
||
| ); | ||
|
|
||
| [ | ||
| { input: [-9, 9], expected: 0 }, | ||
| { input: [-367, -5, 70, 2], expected: -300 }, | ||
| { input: [-7958463, -100, -202, -6453, 153, 45621], expected: -7919444 }, | ||
| ].forEach(({ input, expected }) => | ||
| it("returns the correct sum for array containing negative numbers", () => | ||
| expect(sum(input)).toBeCloseTo(expected)) | ||
| ); | ||
|
|
||
| [ | ||
| { input: [-9, 9, 0.1], expected: 0.1 }, | ||
| { input: [-367, -5, 70, 2, -4.567], expected: -304.567 }, | ||
| { | ||
| input: [-7958463, -100, -202, -6453, 153, 45621, -1.48, 8976.456], | ||
| expected: -7910469.024, | ||
| }, | ||
| ].forEach(({ input, expected }) => | ||
| it("returns the correct sum for array containing decimal/float numbers", () => | ||
| expect(sum(input)).toBeCloseTo(expected)) | ||
| ); | ||
|
|
||
| [ | ||
| { input: [-9, 9, 0.1, () => {}, undefined], expected: 0.1 }, | ||
| { | ||
| input: [-367, -5, "-234", 70, 2, null, { fruit: "apple" }, -4.567], | ||
| expected: -304.567, | ||
| }, | ||
| { input: [Infinity, -Infinity, 1, , NaN, 10, -9], expected: 2 }, | ||
| { | ||
| input: [ | ||
| NaN, | ||
| -7958463, | ||
| -100, | ||
| "Iran", | ||
| -202, | ||
| -6453, | ||
| "UK", | ||
| 153, | ||
| [], | ||
| 45621, | ||
| "Egypt", | ||
| -1.48, | ||
| 8976.456, | ||
| {}, | ||
| ], | ||
| expected: -7910469.024, | ||
| }, | ||
| ].forEach(({ input, expected }) => | ||
| it("returns the correct sum for array containing finite and infinite values", () => | ||
| expect(sum(input)).toBeCloseTo(expected)) | ||
| ); | ||
|
|
||
| [ | ||
| ["not an array", null, undefined, {}, []], | ||
| [("apple", null, undefined)], | ||
| ].forEach((item) => | ||
| it("returns 0 for arrays with only non-number values", () => | ||
| expect(sum(item)).toBeCloseTo(0)) | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| const fs = require("fs"); | ||
|
|
||
| function calculateFrequecy() { | ||
| const text = fs.readFileSync("input.txt", "utf-8"); | ||
| const changesInFrequency = text.trim().split(/\r?\n/).map(Number); | ||
| return changesInFrequency.reduce((acc, curr) => acc + curr, 0); | ||
| } | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.log(calculateFrequecy()); | ||
Uh oh!
There was an error while loading. Please reload this page.