-
-
Notifications
You must be signed in to change notification settings - Fork 286
Sheffield | 26-Jan-ITP | Daniel Aderibigbe | Sprint 1 | Coursework/sprint 1 #986
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 5 commits
e628a41
e2f4c99
d128741
45812d4
e0ff101
c96038b
cc033e1
68bc3bb
b98c6f1
527666a
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,12 @@ | ||
| function dedupe() {} | ||
| function dedupe(array) { | ||
| const result = []; | ||
|
|
||
| for (let i = 0; i < array.length; i++) | ||
| if (!result.includes(array[i])) { | ||
| result.push(array[i]); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function findMax(elements) { | ||
| let max = -Infinity; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number" && elements[i] > max) { | ||
| max = elements[i]; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
Dan2Clouted marked this conversation as resolved.
|
||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function sum(elements) { | ||
| let total = 0; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number") { | ||
| total += elements[i]; | ||
| } | ||
| } | ||
| return total; | ||
| } | ||
|
Dan2Clouted marked this conversation as resolved.
|
||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,71 @@ const sum = require("./sum.js"); | |
| // Given an empty array | ||
| // When passed to the sum function | ||
| // Then it should return 0 | ||
| test.todo("given an empty array, returns 0") | ||
| test("given an empty array, returns 0", () => { | ||
| const input = []; | ||
| const expectedOutput = 0; | ||
| expect(sum(input)).toBe(expectedOutput); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("given an array with just one number, returns that number", () => { | ||
| const input = [5]; | ||
| const expectedOutput = 5; | ||
| expect(sum(input)).toBe(expectedOutput); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
| test("given an array containing negative numbers, it returns the correct total sum", () => { | ||
| const input = [10, -5, 20, -3]; | ||
| const expectedOutput = 22; | ||
| expect(sum(input)).toBe(expectedOutput); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("given an array with decimal numbers, it returns the correct total sum", () => { | ||
| const input = [1.5, 2.3, 0.7]; | ||
| const expectedOutput = 4.5; | ||
| expect(sum(input)).toBe(expectedOutput); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("given an array with decimal numbers, it returns the correct total sum", () => { | ||
| const input = [1.5, 2.3, 0.7]; | ||
| const expectedOutput = 4.5; | ||
| expect(sum(input)).toBe(expectedOutput); | ||
| }); | ||
|
Dan2Clouted marked this conversation as resolved.
|
||
|
|
||
| // 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 | ||
| test("given an array containing non-number values, it ignores them and returns the sum of the numerical elements", () => { | ||
| const input = [10, "hello", 20, null, 5]; | ||
| const expectedOutput = 35; | ||
| expect(sum(input)).toBe(expectedOutput); | ||
| }); | ||
|
|
||
| // 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 | ||
| test("given an array containing non-number values, it ignores them and returns the sum of the numerical elements", () => { | ||
| const input = ["hello", "world"]; | ||
| const expectedOutput = 0; | ||
| expect(sum(input)).toBe(expectedOutput); | ||
| }); | ||
|
|
||
| // 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 | ||
| test("given an array with only non-number values, it returns the least surprising value", () => { | ||
| const input = ["hello", "world"]; | ||
| const expectedOutput = 0; | ||
| expect(sum(input)).toBe(expectedOutput); | ||
| }); | ||
|
Contributor
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. Duplicate |
||
|
Contributor
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 think this exercise also involves "reading data from files". If you are up to the challenge, can you write a script that can read the numbers from a file and then output their sums? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| function sum(list) { | ||
| return list.reduce((a, b) => a + b, 0); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.