Skip to content

feat(contains): create exercise #442

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
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
dfca479
feat/ add new exercise
nik-rev Mar 23, 2024
329d7d6
feat/ update README
nik-rev Mar 23, 2024
897b2c1
feat/ create tests
nik-rev Mar 23, 2024
59c6768
feat/ create solution
nik-rev Mar 23, 2024
ac605aa
feat/ object is not nested
nik-rev Mar 24, 2024
7146b0d
feat/ improve wording
nik-rev Mar 24, 2024
e4e015d
feat/ wording
nik-rev Mar 24, 2024
90afe10
feat/ wording
nik-rev Mar 24, 2024
cd0a557
feat/ improve readability of solution
nik-rev Mar 24, 2024
db402f7
Merge branch 'feat/exercise_14' of https://github.com/nikitarevenco/j…
nik-rev Mar 24, 2024
15e4bec
feat/ mention that explicit NaN check is not required
nik-rev Mar 24, 2024
c19c1bc
feat/ clarify that we would normally use math.isnan
nik-rev Mar 24, 2024
4873c5a
Update 14_contains/solution/contains-solution.spec.js
nik-rev Mar 24, 2024
360078c
feat/ null check
nik-rev Mar 24, 2024
8c5851d
feat: improve clarity of suggestion
nik-rev Mar 30, 2024
fc2fc72
feat: remove mention of IEEE
nik-rev Mar 30, 2024
019af1d
feat: solution now accounts for children nested objects
nik-rev Mar 30, 2024
ee12fc4
feat: add test to account for children nested objects
nik-rev Mar 30, 2024
cc29bbd
feat: use Josh's solution that uses `some` method
nik-rev Apr 7, 2024
0f87177
feat(contains): add some basic examples
nik-rev Aug 18, 2024
e630a8b
feat(contains): add a more exhaustive test for reference in the object
nik-rev Aug 18, 2024
8829dde
feat(contains): move same reference test to the beginning
nik-rev Aug 20, 2024
de104e3
feat(contains): more concise wording
nik-rev Aug 20, 2024
a204931
feat(contains): copy over tests from solution to spec file
nik-rev Aug 20, 2024
d1c8f2c
fix(contains): change false to true for example which is true
nik-rev Aug 23, 2024
2bc1c95
fix: spelling mistake
nik-rev Feb 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions 14_contains/solution/contains-solution.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
const contains = function (obj, searchValue) {
const values = Object.values(obj);
const contains = function (initialObject, searchValue, queue = []) {
if (initialObject !== null) queue.push(initialObject);
if (initialObject === null && queue.length === 0) return false;
const item = queue.shift();
const values = Object.values(item);

// 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 ¯\_(ツ)_/¯
(value) => typeof value === "object" && value !== null,
);
for (const nestedObject of nestedObjects) {
return contains(nestedObject, searchValue);
}

return false;
const newQueue = queue.concat(nestedObjects);

return contains(null, searchValue, newQueue);
};

// Do not edit below this line
Expand Down