-
-
Notifications
You must be signed in to change notification settings - Fork 286
Manchester | 26-ITP-Jan | Liban Jama | Sprint 2 | Data Groups #1050
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8eb514b
Completed sprint-2- implement
libanj0161 cf24d94
Completed Sprint2-debug
libanj0161 8e13b9e
Completed Interpret
libanj0161 faa6d1b
Completed interpret
libanj0161 f48667a
amended after pr comments
libanj0161 8dc6d67
Merge branch 'main' into Data-Groups-Sprint-2
libanj0161 3680a9c
fixed as per pr comments
libanj0161 a0e7c95
removed .only
libanj0161 8ff0784
Updated tally.js as per pr request
libanj0161 0d29926
Added a rejection for null and array, and added test as per pr comment
libanj0161 821b353
made contains work with map
libanj0161 fe9e5cc
updated
libanj0161 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,9 @@ | ||
| function contains() {} | ||
| function contains(object, propertyName) { | ||
| if (Object.prototype.toString.call(object) !== "[object Object]") { | ||
| // if user passes something that is not an object, then return false. | ||
| return false; | ||
| } | ||
| return Object.hasOwn(object, propertyName); | ||
| } | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,30 @@ as the object doesn't contains a key of 'c' | |
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("contains an empty object returns false", () => { | ||
| expect(contains({}, "ball")).toEqual(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
|
|
||
| test("contains an object with properties, returns true", () => { | ||
| expect(contains({ foot: "ball" }, "foot")).toEqual(true); | ||
| }); | ||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("should return false when the object does not contain the property", () => { | ||
| expect(contains({ foot: "ball" }, "basket")).toEqual(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("should return false when input is not an object", () => { | ||
| expect(contains([], "a")).toEqual(false); | ||
| }); | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| test("should throw an error when input is an array even if propertyName is a valid key", () => { | ||
|
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. The function does not throw any error.
Author
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. updated test description |
||
| expect(contains(["x", "y"], "0")).toEqual(false); | ||
| }); | ||
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,19 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(list) { | ||
| // key = country code e.g. 'US' | ||
| // value = currency code e.g. 'USD' | ||
| // input = array of [country, currency] | ||
| // process = go through each pair, add to object | ||
| // output = object { country: currency } | ||
|
|
||
| // declare a variable and store empty object | ||
| // loop through the array | ||
| // while in the loop take each item and add it to the object | ||
| let items = {}; | ||
|
|
||
| for (let i = 0; i < list.length; i++) { | ||
| items[list[i][0]] = list[i][1]; | ||
| } | ||
| return items; | ||
| } | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| 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,16 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| const plainObject = Object.create(null); | ||
|
|
||
| const itemsObject = items.reduce((acc, item) => { | ||
| acc[item] = (acc[item] || 0) + 1; | ||
| return acc; | ||
| }, plainObject); | ||
|
|
||
| return itemsObject; | ||
| } | ||
|
|
||
| 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
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const invert = require("../interpret/invert"); | ||
|
|
||
| test(`Should return {"10": "x", "20": "y"} when given { x: 10, y: 20 } `, () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| }); | ||
|
|
||
| test(`Should return {"2": "bounce", "100": "high"} when given { bounce: 2, high: 100 } `, () => { | ||
| expect(invert({ bounce: 2, high: 100 })).toEqual({ | ||
| 2: "bounce", | ||
| 100: "high", | ||
| }); | ||
| }); | ||
|
|
||
| test(`Should return {"foot": "ball"} when given { "ball": "foot" } `, () => { | ||
| expect(invert({ ball: "foot" })).toEqual({ foot: "ball" }); | ||
| }); |
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 check will reject other kinds of objects such as Map and Set objects.
Can you change it to reject only arrays or any value that is not an object?
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.
Added a rejection for null and array, and added test.
Made contains work with map/set.