-
-
Notifications
You must be signed in to change notification settings - Fork 178
London | 25-ITP-Sep |Imran Mohamed | Sprint 1 | Sprint-1 exercises #802
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
600fa9d
03c0785
c3da981
2d73b61
332dc0c
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(arr) { | ||
| if(!arr.length) return []; | ||
| const result = []; | ||
| arr.forEach(item => { | ||
| if(!result.includes(item)) { | ||
| result.push(item); | ||
| } | ||
| }); | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = dedupe; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| const dedupe = require("./dedupe.js"); | ||
| const dedupe = require('./dedupe'); | ||
|
|
||
| /* | ||
| Dedupe Array | ||
|
|
||
|
|
@@ -16,12 +17,22 @@ 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"); | ||
| 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 a copy of the original array", () => { | ||
| expect(dedupe([1,2,3])).toEqual([1,2,3]); | ||
| }); | ||
|
Comment on lines
+27
to
+29
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. The current test code cannot check if the returned array is a copy of the original array because In order to check if the returned array is a copy of the original array, we would need additional checks. |
||
|
|
||
| // 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 removes duplicate values preserving the first occurrence of each element", () => { | ||
| expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']); | ||
| expect(dedupe([1,1,2,2,3,1,2,4,5,5])).toEqual([1,2,3,4,5]); | ||
| expect(dedupe([1,2,1])).toEqual([1,2]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,11 @@ | ||
| function findMax(elements) { | ||
| function findMax(arr) { | ||
| let max = -Infinity; | ||
| for (let i = 0; i < arr.length; i++) { | ||
| if (typeof arr[i] === 'number' && arr[i] > max) { | ||
| max = arr[i]; | ||
| } | ||
| } | ||
| return max; | ||
| } | ||
|
|
||
| module.exports = findMax; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| function sum(elements) { | ||
| function sum(arr) { | ||
| return arr.reduce((acc, curr) => { | ||
| if (typeof curr === 'number') { | ||
| return acc + curr; | ||
| } | ||
| return acc; | ||
| }, 0); | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,41 @@ 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",()=>{ | ||
| expect(sum([])).toBe(0); | ||
| }) | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("given an array with one number, returns that number",()=>{ | ||
| expect(sum([20])).toBe(20); | ||
| }) | ||
|
|
||
| // 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 with negative numbers, returns correct sum",()=>{ | ||
| expect(sum([-5,-10,-15])).toBe(-30); | ||
| }) | ||
|
|
||
| // 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, returns correct sum",()=>{ | ||
| expect(sum([1.5,2.5,3.5])).toBe(7.5); | ||
| }) | ||
|
Comment on lines
+37
to
+39
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. Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of So the following could happen Can you find a more appropriate way to test a value (that involves decimal number calculations) for equality? Suggestion: Look up
|
||
|
|
||
| // 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 with non-number values, ignores them and returns correct sum",()=>{ | ||
| expect(sum([10,'hello',20,['test'],30,{a:'a'}])).toBe(60); | ||
| }) | ||
|
|
||
| // 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, returns 0",()=>{ | ||
| expect(sum(['hello','world',['test'],{a:'a'}])).toBe(0); | ||
| }) | ||
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.
To more consistently format the code, consider using this VSCode built-in feature to auto format the code: https://code.visualstudio.com/docs/languages/javascript#_formatting