Skip to content
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

feat(contains): create exercise #442

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

Write a function that searches for a value in a nested object. It returns true if the object contains that value.
nikitarevenco marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions 14_contains/contains.js
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;
15 changes: 15 additions & 0 deletions 14_contains/contains.spec.js
nikitarevenco marked this conversation as resolved.
Show resolved Hide resolved
MaoShizhong marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const contains = require('./contains');

describe('contains', () => {
test('First test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary

expect(contains()).toBe('');
});

test.skip('Second test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary

expect(contains()).toBe('');
});
});
23 changes: 23 additions & 0 deletions 14_contains/solution/contains-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 ¯\_(ツ)_/¯
nikitarevenco marked this conversation as resolved.
Show resolved Hide resolved
(value) => typeof value === "object" && value !== null,
);

const newQueue = queue.concat(nestedObjects);

return contains(null, searchValue, newQueue);
nikitarevenco marked this conversation as resolved.
Show resolved Hide resolved
};

// Do not edit below this line
module.exports = contains;
55 changes: 55 additions & 0 deletions 14_contains/solution/contains-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const contains = require("./contains-solution");

describe("contains", () => {
const object = {
nikitarevenco marked this conversation as resolved.
Show resolved Hide resolved
data: {
duplicate: "e",
stuff: {
thing: {
banana: NaN,
moreStuff: {
something: "foo",
answer: [42],
},
},
},
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("false if the provided value exists but is not a primitive", () => {
expect(contains(object, [42])).toBe(false);
});
nikitarevenco marked this conversation as resolved.
Show resolved Hide resolved

test("true if NaN is a value within the object", () => {
expect(contains(object, NaN)).toBe(true);
});

test("false if the provided value exists and is null", () => {
expect(contains(object, null)).toBe(true);
});
});