Birmingham | 26-ITP-May | Toluwalase Tiamiyu | Sprint 1 | Exercises - #1295
Birmingham | 26-ITP-May | Toluwalase Tiamiyu | Sprint 1 | Exercises#1295TTiamiyu wants to merge 13 commits into
Conversation
…e test case for consistency
… empty array, single number, negative numbers, decimal numbers, and non-number values
This comment has been minimized.
This comment has been minimized.
5 similar comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| test("given an array with no duplicates, it returns a copy of the original array", () => { | ||
| expect(dedupe([1, 2, 3])).toEqual([1, 2, 3]); | ||
| }); |
There was a problem hiding this comment.
Your function implementation is correct. However, this test could be improved to better ensure
that any future changes continue to align with the expected behavior described on line 29:
Then it should return a copy of the original array
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.
In order to validate the returned array is a different array, we need an additional check.
Can you find out what this additional check is?
There was a problem hiding this comment.
i found that using a .toBe and .not.toBe assertions would work better than .toEqual assertion for this test, i have changed the assertions and also added extra assertions to cater for future changes
There was a problem hiding this comment.
toEqual() and toBe() serve differerent purposes. In this test you need both.
There was a problem hiding this comment.
that is very true, most of the test cases in the .test.js file have both assertions to check the correctness of the values and also protect against just returning the list as is.
| test("given an array with only non-number values, return the least surprising value", () => { | ||
| expect(findMax(["a", "c", "e"])).toBe(-Infinity); | ||
| }); |
There was a problem hiding this comment.
When a string representing a valid numeric literal (for example, "300") is compared to a number,
JavaScript first converts the string into its numeric equivalent before performing the comparison.
As a result, the expression 20 < "300" evaluates to true.
To test if the function can correctly ignore non-numeric values, you may want to include a string such as "300" in the relevant test cases.
There was a problem hiding this comment.
thank you for pointing that out, i did not think of that and i have adjusted the test case to include a string with numbers.
| @@ -1,4 +1,6 @@ | |||
| function sum(elements) { | |||
| const numbers = elements.filter((item) => typeof item === "number"); | |||
There was a problem hiding this comment.
What do you expect from the following function calls (on extreme cases)?
Does your function return the value you expected?
sum([NaN, 1]);
sum([Infinity, -Infinity]);
There was a problem hiding this comment.
No, it wouldn’t return what is expect, i have Incorporated these extreme cases in the test and also improved the sum.js code to cater for such extremes.
Self checklist