-
-
Notifications
You must be signed in to change notification settings - Fork 153
Sheffield | 25-ITP-May | Declan Williams | Data-Groups-Sprint 1 #695
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
5decc55
5b12f55
1c395d6
11a010f
589fef7
fc0d9bc
4199529
df0b19c
a93c361
88ef053
00abcc4
1e3002d
d5cc4cf
5fde149
b07f167
128a345
05d5b01
f630baa
2235414
4bcc171
f84d2f2
e78614b
ff06a57
f0d4e09
426d205
4221f01
b656247
5e62085
de951df
d87cea0
0ab4dfe
93778e0
cabb9a6
da4dd6c
f9e39f9
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,17 @@ | ||
function dedupe() {} | ||
function dedupe(array) { | ||
// makes a new array to store non duplicate values | ||
const newArray = []; | ||
|
||
// loops over each item in the original array | ||
for (const item of array) { | ||
// checks if the values are not in the "newArray" | ||
if (!newArray.includes(item)) { | ||
// if item is not in "newArray" adds it to the array | ||
newArray.push(item); | ||
} | ||
} | ||
// returns the new deduped array | ||
return newArray; | ||
} | ||
|
||
module.exports = dedupe; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,12 +16,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]); | ||
expect(dedupe(['a', 'b', 'c'])).toEqual(['a', 'b', 'c']); | ||
}); | ||
Comment on lines
+25
to
+28
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 | ||
test("given an array with duplicates it removes any duplicates and preserves the first instance of each element", () => { | ||
expect(dedupe(['a','a','a','b','b','c'])).toEqual(['a','b','c']); | ||
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); | ||
expect(dedupe([1, 2, 1])).toEqual([1, 2]); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,33 @@ | ||
function findMax(elements) { | ||
} | ||
// makes an array to hold only numbers | ||
const numbersOnly = [] | ||
|
||
// to loop through each element in the array | ||
for (const item of elements){ | ||
|
||
// if the item is a number push it to the numbersOnly array | ||
if (typeof item === `number`){ | ||
numbersOnly.push(item); | ||
} | ||
} | ||
// start with the first number in the "numbersOnly" array | ||
let max = numbersOnly[0]; | ||
|
||
// if the no numbers are found in the array returns -infinty | ||
if (numbersOnly.length === 0) { | ||
return -Infinity; | ||
} | ||
Comment on lines
+17
to
+19
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. It is a better practice to perform length check before line 14. In some other programming languages, line 14 could have caused a runtime error when the array has no elements. |
||
|
||
// loop for the largest number in the "numbersOnly" array | ||
for (const num of numbersOnly) { | ||
if (num > max) { | ||
max = num; | ||
} | ||
} | ||
return max; | ||
|
||
} | ||
console.log(findMax([30, 50, 10, 40])); // 50 | ||
console.log(findMax(['hey', 10, 'hi', 60, 10])); // 60 | ||
console.log(findMax([])); // -infinity | ||
module.exports = findMax; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
function sum(elements) { | ||
if (elements.length === 0) return 0; // if array is input is empty returns 0 | ||
|
||
let total = 0; // create a variable to hold the sum | ||
|
||
// iterate over each element in the array | ||
for (const value of elements){ | ||
// checks if each of the values are numbers | ||
if (typeof value === `number`){ | ||
// adds the numbers to the total sum | ||
total += value; | ||
} | ||
} | ||
return total; // returns the total sum after the loop has finished adding all the numbers | ||
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. Indentation of the code at lines 2 and 14 is a bit off. Have you tried the Prettier VSCode extention? |
||
} | ||
|
||
module.exports = sum; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,24 +13,42 @@ 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 a single number return that number", () => { | ||
expect(sum([42])).toBe(42); | ||
}) | ||
|
||
// 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 the correct total sum", () => { | ||
expect(sum([-10, -20, -30])).toBe(-60); | ||
expect(sum([-10, 20, -30])).toBe(-20); | ||
}); | ||
// 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 in returns the correct total sum", () => { | ||
expect(sum([10.5, 20.2, 30.3])).toBe(61.0); | ||
expect(sum([10.5, -20.2, 30.3])).toBe(20.6); | ||
}); | ||
Comment on lines
+37
to
+40
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 containing non-numerical values ignore non-numerical and returns correct total sum", () => { | ||
expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBe(29.9); | ||
}); | ||
|
||
// 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-numerical values returns 0", () => { | ||
expect(sum(["a", "b", "c"])).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.
Infinity
is also a value of type "number".If you want to also reject such numbers, you can use
Number.isFinite()
.