-
Notifications
You must be signed in to change notification settings - Fork 39.4k
feat(contains): create exercise #442
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Open
nik-rev
wants to merge
26
commits into
TheOdinProject:main
Choose a base branch
from
nik-contrib:feat/exercise_14
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
26 commits
Select commit
Hold shift + click to select a range
dfca479
feat/ add new exercise
nik-rev 329d7d6
feat/ update README
nik-rev 897b2c1
feat/ create tests
nik-rev 59c6768
feat/ create solution
nik-rev ac605aa
feat/ object is not nested
nik-rev 7146b0d
feat/ improve wording
nik-rev e4e015d
feat/ wording
nik-rev 90afe10
feat/ wording
nik-rev cd0a557
feat/ improve readability of solution
nik-rev db402f7
Merge branch 'feat/exercise_14' of https://github.com/nikitarevenco/j…
nik-rev 15e4bec
feat/ mention that explicit NaN check is not required
nik-rev c19c1bc
feat/ clarify that we would normally use math.isnan
nik-rev 4873c5a
Update 14_contains/solution/contains-solution.spec.js
nik-rev 360078c
feat/ null check
nik-rev 8c5851d
feat: improve clarity of suggestion
nik-rev fc2fc72
feat: remove mention of IEEE
nik-rev 019af1d
feat: solution now accounts for children nested objects
nik-rev ee12fc4
feat: add test to account for children nested objects
nik-rev cc29bbd
feat: use Josh's solution that uses `some` method
nik-rev 0f87177
feat(contains): add some basic examples
nik-rev e630a8b
feat(contains): add a more exhaustive test for reference in the object
nik-rev 8829dde
feat(contains): move same reference test to the beginning
nik-rev de104e3
feat(contains): more concise wording
nik-rev a204931
feat(contains): copy over tests from solution to spec file
nik-rev d1c8f2c
fix(contains): change false to true for example which is true
nik-rev 2bc1c95
fix: spelling mistake
nik-rev 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Exercise 14 - contains | ||
|
||
Write a function that searches for a value in a nested object. It returns true if the object contains that value. | ||
|
||
Objects are compared by reference. | ||
|
||
Examples: | ||
|
||
```javascript | ||
contains({ foo: "foo" }, "bar") // false | ||
contains({ foo: { bar: "bar" } }, "bar") // true | ||
``` |
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,6 @@ | ||
const contains = function() { | ||
|
||
}; | ||
|
||
// Do not edit below this line | ||
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const contains = require("./contains"); | ||
|
||
describe("contains", () => { | ||
const meaningOfLifeArray = [42]; | ||
const object = { | ||
data: { | ||
duplicate: "e", | ||
stuff: { | ||
thing: { | ||
banana: NaN, | ||
moreStuff: { | ||
something: "foo", | ||
answer: meaningOfLifeArray, | ||
}, | ||
}, | ||
}, | ||
info: { | ||
duplicate: "e", | ||
magicNumber: 44, | ||
empty: null, | ||
}, | ||
}, | ||
}; | ||
|
||
test("true if the provided number is a value within the object", () => { | ||
expect(contains(object, 44)).toBe(true); | ||
}); | ||
|
||
test.skip("true if the provided string is a value within the object", () => { | ||
expect(contains(object, "foo")).toBe(true); | ||
}); | ||
|
||
test.skip("does not convert input string into a number when searching for a value within the object", () => { | ||
expect(contains(object, "44")).toBe(false); | ||
}); | ||
|
||
test.skip("false if the provided string is not a value within the object", () => { | ||
expect(contains(object, "bar")).toBe(false); | ||
}); | ||
|
||
test.skip("true if provided string is within the object, even if duplicated", () => { | ||
expect(contains(object, "e")).toBe(true); | ||
}); | ||
|
||
test.skip("true if the object contains the same object by reference", () => { | ||
expect(contains(object, meaningOfLifeArray)).toBe(true); | ||
}); | ||
|
||
test.skip("false if no matching object reference", () => { | ||
expect(contains(object, [42])).toBe(false); | ||
}); | ||
|
||
test.skip("true if NaN is a value within the object", () => { | ||
expect(contains(object, NaN)).toBe(true); | ||
}); | ||
|
||
test.skip("false if the provided value exists and is null", () => { | ||
expect(contains(object, null)).toBe(true); | ||
}); | ||
}); |
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,20 @@ | ||
const contains = function (object, searchValue) { | ||
const values = Object.values(object); | ||
|
||
// NaN === NaN evaluates to false | ||
// Normally, we would have to do an explicit Number.isNaN() check to compare NaN equality | ||
// However, Array.prototype.includes automatically handles this for us | ||
if (values.includes(searchValue)) return true; | ||
|
||
const nestedObjects = values.filter( | ||
// typeof null === 'object' evaluates to true ¯\_(ツ)_/¯ | ||
nik-rev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(value) => typeof value === "object" && value !== null | ||
); | ||
|
||
return nestedObjects.some((nestedObject) => | ||
contains(nestedObject, searchValue) | ||
); | ||
}; | ||
|
||
// Do not edit below this line | ||
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const contains = require("./contains-solution"); | ||
|
||
describe("contains", () => { | ||
const meaningOfLifeArray = [42]; | ||
const object = { | ||
nik-rev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data: { | ||
duplicate: "e", | ||
stuff: { | ||
thing: { | ||
banana: NaN, | ||
moreStuff: { | ||
something: "foo", | ||
answer: meaningOfLifeArray, | ||
}, | ||
}, | ||
}, | ||
info: { | ||
duplicate: "e", | ||
magicNumber: 44, | ||
empty: null, | ||
}, | ||
}, | ||
}; | ||
|
||
test("true if the provided number is a value within the object", () => { | ||
expect(contains(object, 44)).toBe(true); | ||
}); | ||
|
||
test("true if the provided string is a value within the object", () => { | ||
expect(contains(object, "foo")).toBe(true); | ||
}); | ||
|
||
test("does not convert input string into a number when searching for a value within the object", () => { | ||
expect(contains(object, "44")).toBe(false); | ||
}); | ||
|
||
test("false if the provided string is not a value within the object", () => { | ||
expect(contains(object, "bar")).toBe(false); | ||
}); | ||
|
||
test("true if provided string is within the object, even if duplicated", () => { | ||
expect(contains(object, "e")).toBe(true); | ||
}); | ||
|
||
test("true if the object contains the same object by reference", () => { | ||
expect(contains(object, meaningOfLifeArray)).toBe(true); | ||
}); | ||
|
||
test("false if no matching object reference", () => { | ||
expect(contains(object, [42])).toBe(false); | ||
}); | ||
|
||
test("true if NaN is a value within the object", () => { | ||
expect(contains(object, NaN)).toBe(true); | ||
}); | ||
|
||
test("true if the provided value exists and is null", () => { | ||
expect(contains(object, null)).toBe(true); | ||
}); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.