Skip to content

Commit 019af1d

Browse files
committed
feat: solution now accounts for children nested objects
1 parent fc2fc72 commit 019af1d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

14_contains/solution/contains-solution.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
const contains = function (obj, searchValue) {
2-
const values = Object.values(obj);
1+
const contains = function (initialObject, searchValue, queue = []) {
2+
if (initialObject !== null) queue.push(initialObject);
3+
if (initialObject === null && queue.length === 0) return false;
4+
const item = queue.shift();
5+
const values = Object.values(item);
36

47
// NaN === NaN evaluates to false
58
// Normally, we would have to do an explicit Number.isNaN() check to compare NaN equality
69
// However, Array.prototype.includes automatically handles this for us
710
if (values.includes(searchValue)) return true;
811

912
const nestedObjects = values.filter(
13+
// typeof null === 'object' evaluates to true ¯\_(ツ)_/¯
1014
(value) => typeof value === "object" && value !== null,
1115
);
12-
for (const nestedObject of nestedObjects) {
13-
return contains(nestedObject, searchValue);
14-
}
1516

16-
return false;
17+
const newQueue = queue.concat(nestedObjects);
18+
19+
return contains(null, searchValue, newQueue);
1720
};
1821

1922
// Do not edit below this line

0 commit comments

Comments
 (0)