-
-
Notifications
You must be signed in to change notification settings - Fork 270
Birmingham | ITP-Jan | Roman Sanaye | Sprint 1 | Sprint 1 Exercises #996
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
3feedc9
77b6546
13cba99
d56b099
0da8e5e
b893fbd
e87037f
c212933
9fb887c
4a6f7d0
065ae9e
1e3a99e
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() {} | ||
| // start with the function; | ||
| function dedupe(arr) { | ||
| if (arr.length === 0) return []; | ||
| const cleanArr = []; | ||
| for (let item of arr) { | ||
| if (!cleanArr.includes(item)) { | ||
| cleanArr.push(item); | ||
| } | ||
Poonam-raj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return cleanArr; | ||
| } | ||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ const dedupe = require("./dedupe.js"); | |
| Dedupe Array | ||
|
|
||
| 📖 Dedupe means **deduplicate** | ||
|
|
||
| In this kata, you will need to deduplicate the elements of an array | ||
|
|
||
| E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c'] | ||
|
|
@@ -16,12 +16,23 @@ 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("for empty array it should return an empty array", () => { | ||
| const arr = []; | ||
| expect(dedupe(arr)).toEqual([]); | ||
| }); | ||
|
|
||
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| test("for array with no duplicates should return the original array", () => { | ||
| const arr = [1, 2, 3, 4]; | ||
| expect(dedupe(arr)).toEqual(arr); | ||
| }); | ||
|
Comment on lines
26
to
+30
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. This test case should consider that the returned array is a copy of the original array. References is a relevant topic here: https://programming.codeyourfuture.io/data-groups/sprints/1/prep/#references How would you check for a copy of the array in the test? 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. @RomanSanaye what did you think of this idea? |
||
|
|
||
| // 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 | ||
| test("removes duplicates and keeps first occurrence", () => { | ||
| const arr = [1, 2, 1, 3, 4]; | ||
| expect(dedupe(arr)).toEqual([1, 2, 3, 4]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,9 @@ | ||
| // start your function; | ||
| function findMax(elements) { | ||
| if (elements.length === 0) return -Infinity; | ||
| let onlyNumArr = elements.filter((el) => typeof el === "number"); | ||
Poonam-raj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let maxNum = Math.max(...onlyNumArr); | ||
Poonam-raj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return maxNum; | ||
| } | ||
|
|
||
|
Comment on lines
+3
to
8
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 anything be a 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. @RomanSanaye did you have any thoughts on this? |
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,28 +16,55 @@ const findMax = require("./max.js"); | |
| // When passed to the max 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("if given empty array it should return Infinity", () => { | ||
| const elements = []; | ||
| expect(findMax(elements)).toEqual(-Infinity); | ||
| }); | ||
|
|
||
| // Given an array with one number | ||
| // When passed to the max function | ||
| // Then it should return that number | ||
| test("if given 1 number, it should return that number", () => { | ||
| const elements = [8]; | ||
| expect(findMax(elements)).toEqual(8); | ||
| }); | ||
|
|
||
| // Given an array with both positive and negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest number overall | ||
| test("given positive and negative value, should return largest number overall", () => { | ||
| const elements = [1, 3, 4, -3, -5]; | ||
| expect(findMax(elements)).toEqual(4); | ||
| }); | ||
|
|
||
| // Given an array with just negative numbers | ||
| // When passed to the max function | ||
| // Then it should return the closest one to zero | ||
| test("if given all negative number it should return closest to Zero", () => { | ||
| const elements = [-1, -2, -4, -8]; | ||
| expect(findMax(elements)).toEqual(-1); | ||
| }); | ||
|
|
||
| // Given an array with decimal numbers | ||
| // When passed to the max function | ||
| // Then it should return the largest decimal number | ||
| test("if given decimal number, should return largest decimal number", () => { | ||
| const elements = [1.2, 5.5, 9.1]; | ||
| expect(findMax(elements)).toEqual(9.1); | ||
| }); | ||
|
|
||
| // Given an array with non-number values | ||
| // When passed to the max function | ||
| // Then it should return the max and ignore non-numeric values | ||
| test("if given non-number values it should return only max number", () => { | ||
| const elements = [2, 5, 7, "hi", "apple", 9]; | ||
| expect(findMax(elements)).toEqual(9); | ||
| }); | ||
|
|
||
| // Given an array with only non-number values | ||
| // When passed to the max function | ||
| // Then it should return the least surprising value given how it behaves for all other inputs | ||
| test("if given only non-number values it should return -Infinity ", () => { | ||
| const elements = ["a", "n", "hi"]; | ||
| expect(findMax(elements)).toEqual(-Infinity); | ||
| }); | ||
|
Comment on lines
+67
to
+70
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. This choice feels appropriate - good work |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,12 @@ | ||
| function sum(elements) { | ||
| if (elements.length === 0) return 0; | ||
| let numberElement = elements.filter((el) => typeof el === "number"); | ||
| if (numberElement.length === 0) return 0; // least surprising value; | ||
| let total = 0; | ||
| for (let num of numberElement) { | ||
| total += num; | ||
| } | ||
| return Number.isInteger.total ? total : Number(total.toFixed(2)); | ||
| } | ||
|
Comment on lines
1
to
10
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. Again - which variables here need to be a 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'd say this comment was not addressed 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. @RomanSanaye Can numberElement or total be made into |
||
|
|
||
| module.exports = sum; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,47 @@ 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("Empty array should be returned Zero", () => { | ||
| let elements = []; | ||
| expect(sum(elements)).toEqual(0); | ||
| }); | ||
|
|
||
| // Given an array with just one number | ||
| // When passed to the sum function | ||
| // Then it should return that number | ||
| test("given one number should be returned same as original", () => { | ||
| let elements = [5]; | ||
| expect(sum(elements)).toEqual(5); | ||
| }); | ||
|
|
||
| // Given an array containing negative numbers | ||
| // When passed to the sum function | ||
| // Then it should still return the correct total sum | ||
| test("negative number should return total with no effect", () => { | ||
| let elements = [1, 3, -2]; | ||
| expect(sum(elements)).toEqual(2); | ||
| }); | ||
|
|
||
| // Given an array with decimal/float numbers | ||
| // When passed to the sum function | ||
| // Then it should return the correct total sum | ||
| test("decimal number should return the correct total", () => { | ||
| let elements = [1, 2.3, 4.5, 6]; | ||
| expect(sum(elements)).toEqual(13.8); | ||
| }); | ||
|
Comment on lines
+40
to
+43
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. good work! |
||
|
|
||
| // 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("non-number values be ignored, function still should return total", () => { | ||
| let elements = [1, 2, 3, "hi", "b"]; | ||
| expect(sum(elements)).toEqual(6); | ||
| }); | ||
|
|
||
| // 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("non-number values should return Zero", () => { | ||
| let elements = ["a", "d", "hi"]; | ||
| expect(sum(elements)).toEqual(0); | ||
| }); | ||
|
Comment on lines
+56
to
+59
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. Lovely work |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,8 @@ | ||
| // Refactor the implementation of includes to use a for...of loop | ||
| // this new version looks great. | ||
|
|
||
| function includes(list, target) { | ||
| for (let index = 0; index < list.length; index++) { | ||
| const element = list[index]; | ||
| if (element === target) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| return list.includes(target) ? true : false; | ||
Poonam-raj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| module.exports = includes; | ||
Uh oh!
There was an error while loading. Please reload this page.