Repository navigation
London | 26-ITP-Sep | Mahir Shah | Sprint 1 | Data Groups #1481
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
42e6670
b20550f
6af26a2
7a3e4e5
36a519d
e97ee71
57f2875
1b7b599
479a30c
42bdd8d
03f21b0
4d19b02
c3a4ffa
8133c48
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(elements) { | ||
| const dedupedArray = []; | ||
|
|
||
| for (value of elements) { | ||
| if (!dedupedArray.includes(value)) { | ||
| dedupedArray.push(value); | ||
| } | ||
| } | ||
|
Comment on lines
+4
to
+8
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. Should the loop variable |
||
| return dedupedArray; | ||
| } | ||
|
|
||
| module.exports = dedupe; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,16 +13,20 @@ E.g. dedupe([1, 2, 1]) returns [1, 2] | |
|
|
||
| // Acceptance Criteria: | ||
|
|
||
| // 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, should return a copy of the same array", () => { | ||
| expect(dedupe([1, 5, 9, 23])).toEqual([1, 5, 9, 23]); | ||
| }); | ||
|
Comment on lines
-21
to
+22
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. Your function implementation is correct. However, this test could be improved to better ensure
This test should fail if the function returns the original array (instead of a copy of the original array). The current test checks only if both the original array and the returned array contain identical elements. Can you find out what this additional check is? |
||
|
|
||
| // Given an array of strings or numbers | ||
| // When passed to the dedupe function | ||
| // Then it should return a new array with duplicates removed while preserving the | ||
| // first occurrence of each element from the original array. | ||
| test("given an array with strings or numbers, should return an array with duplicates removed and first occurrence preserved", () => { | ||
| expect(dedupe([1, 6, 1, "a", 5, 6, "a", "b", "b"])).toEqual([ | ||
| 1, | ||
| 6, | ||
| "a", | ||
| 5, | ||
| "b", | ||
| ]); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,18 @@ function calculateMedian(list) { | |
| } | ||
|
|
||
| // Implement this function. See describe-median.test.js for the acceptance criteria. | ||
| function describeMedian(list) {} | ||
| function describeMedian(list) { | ||
| try { | ||
| return "The median is " + calculateMedian(list); | ||
| } catch { | ||
| if (list.length === 0) { | ||
| return "Could not calculate a median: calculateMedian requires a non-empty array"; | ||
| } | ||
|
|
||
| if (!Array.isArray(list)) { | ||
| return "Could not calculate a median: calculateMedian requires an array of numbers"; | ||
| } | ||
| } | ||
|
Comment on lines
+27
to
+34
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. Could you look up how to use a |
||
| } | ||
|
|
||
| module.exports = describeMedian; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,24 @@ | ||
| function findMax(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| if (elements.length === 0) { | ||
| return -Infinity; | ||
| } | ||
|
|
||
| if ( | ||
| !elements.every( | ||
| (currentValue) => | ||
| typeof currentValue === "number" && !Number.isNaN(currentValue) | ||
| ) | ||
| ) { | ||
| throw new Error("findMax requires an array of numbers"); | ||
| } | ||
|
|
||
| sortedElements = elements.toSorted((a, b) => a - b); | ||
|
|
||
| return sortedElements[sortedElements.length - 1]; | ||
|
Comment on lines
+19
to
+21
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. This work. Suggestion: Explore a more efficient approach to find the largest numbers in an array. |
||
| } | ||
|
|
||
| module.exports = findMax; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,29 @@ | ||
| function calculateMean(list) {} | ||
| function calculateMean(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
|
|
||
| if (list.length === 0) { | ||
| throw new Error("calculateMean requires a non-empty array"); | ||
| } | ||
|
|
||
| if ( | ||
| !list.every( | ||
| (currentValue) => | ||
| typeof currentValue === "number" && !Number.isNaN(currentValue) | ||
| ) | ||
|
Comment on lines
+11
to
+14
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.
|
||
| ) { | ||
| throw new Error("calculateMean requires an array of numbers"); | ||
| } | ||
|
|
||
| let total = 0; | ||
|
|
||
| for (number of list) { | ||
| total += number; | ||
| } | ||
| const mean = total / list.length; | ||
|
|
||
| return mean; | ||
| } | ||
|
|
||
| module.exports = calculateMean; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,22 @@ | ||
| function sum(elements) { | ||
| if (!Array.isArray(elements)) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
|
|
||
| if ( | ||
| !elements.every( | ||
| (currentValue) => | ||
| typeof currentValue === "number" && !Number.isNaN(currentValue) | ||
| ) | ||
| ) { | ||
| throw new Error("sum requires an array of numbers"); | ||
| } | ||
|
|
||
| let total = 0; | ||
| for (number of elements) { | ||
| total += number; | ||
| } | ||
| return total; | ||
| } | ||
|
|
||
| module.exports = sum; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,27 +10,30 @@ 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 a value that isn't a number | ||
| // When passed to the sum function | ||
| // Then it should throw Error("sum requires an array of numbers") | ||
|
|
||
| // Given something that isn't an array at all, such as "hey", 42 or no argument | ||
| // When passed to the sum function | ||
| // Then it should throw Error("sum requires an array of numbers") | ||
| test("given an empty array, returns 0", () => { | ||
| expect(sum([])).toEqual(0); | ||
| }); | ||
|
|
||
| test("given an array with one number, return that number", () => { | ||
| expect(sum([5])).toEqual(5); | ||
| }); | ||
|
|
||
| test("given an array with negative numbers, correctly return sum of numbers", () => { | ||
| expect(sum([-10, -45, -6, -74])).toEqual(-135); | ||
| }); | ||
|
|
||
| test("given an array with decimal numbers, correctly return total sum", () => { | ||
| expect(sum([12.7, 3.6, 32.48, 8, 32.53])).toEqual(89.31); | ||
| }); | ||
|
Comment on lines
+25
to
+27
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. console.log(12.7 + 3.6 + 32.48 + 8 + 32.53 == 89.31); // true
// When numbers are added in different order
console.log(32.53 + 8 + 32.48 + 3.6 + 12.7 == 89.31); // falseCan you find a more appropriate way to test a value (that involves decimal number calculations) for equality? Suggestion: Look up
|
||
|
|
||
| test("given an array with some values that aren't numbers, throw an Error", () => { | ||
| expect(() => { | ||
| sum([10, 8, 32, "hello", 2, 53, 32]); | ||
| }).toThrow(new Error("sum requires an array of numbers")); | ||
| }); | ||
|
|
||
| test("given something that isn't an array, throw an Error", () => { | ||
| expect(() => { | ||
| sum("hello"); | ||
| }).toThrow(new Error("sum requires an array of numbers")); | ||
| }); | ||
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.
Could also consider excluding +/-
Infinity.Could probably give a simpler name than
currentValueto the parameter. The prefix "current" does not add any new info.