Repository navigation
London | 26-ITP-May | Gideon Defar | sprint 1 | Coursework #1437
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
Open
gideondefar
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
gideondefar:feature/sprint-1-coursework
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d839e3
Fix failing function tests
8d0b020
Implement max function
f448e3e
Implement sum function
3a96f61
Implement dedupe function
99b017e
Refactor functions
e482e09
Fix and complete sprint 1 exercises
55627f4
add sum test for non-number values
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| function dedupe() {} | ||
| function dedupe(arr) { | ||
| return [...new Set(arr)]; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function findMax(elements) { | ||
| const numbersOnly = elements.filter( | ||
| (item) => typeof item === "number" && !Number.isNaN(item) | ||
| ); | ||
| if (numbersOnly.length === 0) { | ||
| return -Infinity; | ||
| } | ||
| const sorted = [...numbersOnly].sort((a, b) => a - b); | ||
| const max = sorted[sorted.length - 1]; | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; | ||
| module.exports = findMax; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,81 @@ | ||
| /* Find the maximum element of an array of numbers | ||
| /* Find the findMaximum element of an array of numbers | ||
|
|
||
| In this kata, you will need to implement a function that find the largest numerical element of an array. | ||
|
|
||
| E.g. max([30, 50, 10, 40]), target output: 50 | ||
| E.g. max(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) | ||
| E.g. findMax([30, 50, 10, 40]), target output: 50 | ||
| E.g. findMax(['hey', 10, 'hi', 60, 10]), target output: 60 (sum ignores any non-numerical elements) | ||
|
|
||
| You should implement this function in max.js, and add tests for it in this file. | ||
| You should implement this function in findMax.js, and add tests for it in this file. | ||
|
|
||
| We have set things up already so that this file can see your function from the other file. | ||
| */ | ||
|
|
||
| const findMax = require("./max.js"); | ||
|
|
||
| // Given an empty array | ||
| // When passed to the max function | ||
| // When passed to the findMax function | ||
| // Then it should return -Infinity | ||
| // Delete this test.todo and replace it with a test. | ||
| test.todo("given an empty array, returns -Infinity"); | ||
|
|
||
| test("returns -Infinity for an empty array", () => { | ||
| const currentOutput = findMax([]); | ||
| const targetOutput = -Infinity; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // When passed to the findMax function | ||
| // Then it should return that number | ||
|
|
||
| test("returns with one number", () => { | ||
| const currentOutput = findMax([3]); | ||
| const targetOutput = 3; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // When passed to the findMax function | ||
| // Then it should return the largest number overall | ||
| test("returns largest number", () => { | ||
| const currentOutput = findMax([3, -10]); | ||
| const targetOutput = 3; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // When passed to the findMax function | ||
| // Then it should return the closest one to zero | ||
| test("return closest to 0", () => { | ||
| const currentOutput = findMax([-7, -3, -10]); | ||
| const targetOutput = -3; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // When passed to the findMax function | ||
| // Then it should return the largest decimal number | ||
| test("return to largest decimal number", () => { | ||
| const currentOutput = findMax([2.5, 5.6]); | ||
| const targetOutput = 5.6; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
| // When passed to the findMax function | ||
| // Then it should return the findMax and ignore non-numeric values | ||
|
|
||
| test("return to findMax and ignore non-numeric values", () => { | ||
| const currentOutput = findMax([7, 4, -10, "b", "f"]); | ||
| const targetOutput = 7; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the max function | ||
| // When passed to the findMax function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("return the least surprising value", () => { | ||
| const currentOutput = findMax([NaN, "hi", true]); | ||
| const targetOutput = -Infinity; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| function sum(elements) { | ||
| let total = 0; | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
| if (typeof elements[i] === "number" && !isNaN(elements[i])) { | ||
| total += elements[i]; | ||
| } | ||
| } | ||
|
|
||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; | ||
| module.exports = sum; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,30 +7,70 @@ E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical | |
| */ | ||
|
|
||
| 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") | ||
|
|
||
| test("returns 0 for empty array", () => { | ||
| const list = []; | ||
| const currentOutput = sum(list); | ||
| const targetOutput = 0; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
|
|
||
| test("sum of 1 number", () => { | ||
| const list = [1]; | ||
| const currentOutput = sum(list); | ||
|
|
||
| const targetOutput = 1; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
|
|
||
| test("sum of negative number in arrays", () => { | ||
| const list = [-5]; | ||
| const currentOutput = sum(list); | ||
| const targetOutput = -5; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
|
|
||
| test("sum of decimal/float number in arrays", () => { | ||
| const list = [4.5, 2.5]; | ||
| const currentOutput = sum(list); | ||
| const targetOutput = 7; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // 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("ignores non-number values", () => { | ||
| const list = ["hey", 10, "hi", 60, 10]; | ||
| const currentOutput = sum(list); | ||
| const targetOutput = 80; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| test("ignores NaN values when calculating the sum", () => { | ||
| const list = [4, NaN, 6]; | ||
| const currentOutput = sum(list); | ||
| const targetOutput = 10; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // 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 | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
|
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. Missing test case for this one and please add it as well. Thank you. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,4 +10,4 @@ function includes(list, target) { | |
| return false; | ||
| } | ||
|
|
||
| module.exports = includes; | ||
| module.exports = includes; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There are two cases in the "ignores non-number values" suite and both are odd number of elements aftrer filtering. Maybe consider next time also have even number of elements after filtering. I will let you pass this time since this is just a minor issue and your other test cases have good coverage.