West Midlands | 26-Jan-ITP | Fida Ali Zada | Sprint 1 | Data Groups#982
West Midlands | 26-Jan-ITP | Fida Ali Zada | Sprint 1 | Data Groups#982alizada-dev wants to merge 12 commits intoCodeYourFuture:mainfrom
Conversation
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
|
|
||
| test("if no duplicates, return the copy of the original array", () => { | ||
| expect(dedupe([1, 3, 5, 7])).toEqual([1, 3, 5, 7]); | ||
| }) | ||
|
|
There was a problem hiding this comment.
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.
By implementing return [...new Set(array)];, the test will pass correctly, because this implementation always returns a new array.
There was a problem hiding this comment.
The concern here is not about your function implementation -- it was correct.
The issue is, how to prepare a test to check if the function is indeed returning a copy of the original array.
With the test you had, a function like this could also pass the test.
function dedupe(array) {
const set = new Set(array);
if (set.size == array.length) return array;
return [...set];
} There was a problem hiding this comment.
const input = [1, 3, 5, 7];
const result = dedupe(input);
expect(result).toEqual(input);
expect(result).not.toBe(input);
There was a problem hiding this comment.
With this test, there is a chance that, even though result has incorrect elements (for example, []),
the two tests could still pass. Can you figure out why, and then fix the tests accordingly?
There was a problem hiding this comment.
const input = [1, 3, 5, 7];
const result = dedupe(input);
expect(result).toEqual([1, 3, 5, 7]);
expect(result).not.toBe([1, 3, 5, 7]);
Is it alright, this time?
There was a problem hiding this comment.
Still not correct. Try looking up the difference between toBe() and toEqual().
|
The proposed test in All other changes look good. |
|
Closing PR because the January ITP run has finished. Feel free to re-open if you're still working on it. |
Learners, PR Template
Self checklist
Changelist
Completed all the exercises of sprint 1 from the Data Group module.