-
-
Notifications
You must be signed in to change notification settings - Fork 279
London | 26-ATP-Jan | Boualem Larbi Djebbour | Sprint 2 | coursework #1188
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
Open
djebsoft
wants to merge
15
commits into
CodeYourFuture:main
Choose a base branch
from
djebsoft:sprint-2/coursework
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5f15309
predicting the output and debugging the code.
djebsoft 5638fc4
predicting and debugging
djebsoft 70fe1ba
predicting and debugging
djebsoft 648ae28
implements a function that returns true if the object has a given pro…
djebsoft d76a207
implemented the createLookup function
djebsoft dcafc3b
cover missing case
djebsoft 2830956
completed the todo tests
djebsoft 611d247
complted sprint-2 coursework
djebsoft 0dddbf3
fixed contains function
djebsoft e903450
changed the name of the parameter
djebsoft 5eee074
throw an error instead
djebsoft df82c4f
fixed all errors to pass all tests
djebsoft 6a6d255
adjusted the function name
djebsoft 57fab61
updated the array input test.
djebsoft 5f43729
udated the array input test
djebsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| function contains() {} | ||
| function contains(obj, name) { | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
| return Object.hasOwn(obj, name); | ||
| } | ||
|
|
||
| module.exports = contains; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,24 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| // the first element is the country code | ||
| // the second array is the currency code of the country | ||
| // the function should return an object | ||
| // the keys of the object should be the first element of each inner array (country code) | ||
| // the values of the object should be the second element of each inner array (currency code) | ||
|
|
||
| function createLookup(countryCurrencyPairs) { | ||
| // check if the argument is an array of arrays | ||
| if ( | ||
| !Array.isArray(countryCurrencyPairs) || | ||
| // check if each inner array is an array. | ||
| countryCurrencyPairs.some((pair) => !Array.isArray(pair)) || | ||
| // check if each inner array has two elements. | ||
| countryCurrencyPairs.some((pair) => pair.length !== 2) | ||
| // we can also use index to check the pair is an array as follows: | ||
| // for (let i = 0; i < countryCurrencyPairs.length; i++) | ||
| // return Array.isArray(countryCurrencyPairs[i]) | ||
| ) { | ||
| throw new Error("Invalid input: expected an array of arrays"); | ||
| } | ||
| return Object.fromEntries(countryCurrencyPairs); | ||
| } | ||
|
|
||
| module.exports = createLookup; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("invalid input"); | ||
| } | ||
| // using Object.create(null) to create a plain object without prototype | ||
| let result = Object.create(null); | ||
| for (let i = 0; i <= arr.length - 1; i++) { | ||
| let item = arr[i]; | ||
| result[item] = (result[item] || 0) + 1; | ||
| } | ||
| return result; | ||
| } | ||
| module.exports = tally; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This test does not yet confirm that the function correctly returns false when the first argument is an array.
This is because
contains(["gitName", "position"], "gitName")could also returnfalsesimply because "gitName" is not a key of the array.Arrays are objects, with their indices acting as keys. A proper test should use a valid
key to ensure the function returns
falsespecifically because the input is an array, not because the key is missing.After you fixed this test, make sure you also run the test to check your function.
Note: When testing invalid type of data,
undefinedandnullare usually good candidates to test -- many functions fail because they could not handleundefinedornull.Uh oh!
There was an error while loading. Please reload this page.
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.
thank you for the feedback
I just realised that the false returned cases should be the exception, so I changed the if condition to the other way around.
I removed the case of empty object (object length equals to zero) as it does not add any plus to the code.
I also added the case where the input is an array to return false.
and run the test to check the function.
added a test with null input for invalid type of data.
Uh oh!
There was an error while loading. Please reload this page.
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.
Why not also update the test (the one that tests array)?
Your function is correct, but we write tests not only to verify our current implementation, but also to ensure that future changes do not alter the function's expected behavior.
Uh oh!
There was an error while loading. Please reload this page.
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.
thanks for the spot
sorry that I got confused about this point
i think the perfect example test is to test tricky arguments where the first argument looks like an object by having two elements and the second argument is string that match one the elements in the first argument
therefore, if the first argument wasn't an array than the test should return true. but because it an array, the function returns false as invalid input.
could you enlighten me about what I'm mistaken here ?
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.
expect(contains(["gitName", "position"], "gitName")).toEqual(false);The following function (changed in the future by someone else) could also pass the above test:
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.
This test is supposed to fail when the function is implemented as:
The new test still behaves the same as the previous one.
Arrays are objects, with their indices acting as keys. You should use a valid
key to ensure the function returns false specifically because the input is an array, not because the key is missing.