8 hours on this (Wherefore art thou)

Challenge: Wherefore art thou | freeCodeCamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

Hi-hi!

For Intermediate Algorithm Scripting section, I said to myself that I will never look at that answers or hints. Now I’m on 4th challenge.

I know I was on this challenge at least 8 hours (Without counting rests). Maybe 11. I stopped working on it to get guidance from you.

I have read this important sentence before multiple times that I must break problem into smaller things, and also I used pen and paper.

My program:

function whatIsInAName(collection, source)
{
        let Output = [];

        let sourceKeys = Object.keys(source);
        let sourceValues = Object.values(source);

        for (let i = 0; i < collection.length; i++)
        {
                let collectionKeys = Object.keys(collection[i]);
                let collectionValues = Object.values(collection[i]);

                let Counter = 0;

                for (let j = 0; j < sourceKeys.length; j++)
                {
                        let KeySearch = collectionKeys.includes(sourceKeys[j]);
                        let ValueSearch = collectionValues.includes(sourceValues[j]);

                        if (KeySearch === true && ValueSearch === true)
                        {
                                Counter++;
                        }
                }

                if (Counter === sourceKeys.length)
                {
                        Output.push(collection[i]);
                }
        }

        return Output;
}

Code explanation:

  • I got keys and values of second argument (source) object in two variables (sourceKeys and sourceValues).
  • Each time program gets an object from first argument array by iterating loop, it will create a Counter variable, and if a single pair of second argument key and value exist in this object, Counter will be increased by 1.
    So now one pair checked, then it will check all pairs exist or no by if (Counter === sourceKeys.length).

My code passes all tests except last one; Reason :

whatIsInAName([{"a": 1, "b": 2, "c": 3, "d": 9999}], {"a": 1, "b": 9999, "c": 3});

"b": 9999 in second argument. My code checks is "b" in first arg? Yes. Is 9999 in first arg? Yes. So it say "b": 9999 exist in first arg.
There is no connection between keys and values in my code.
The includes() method returns true by finding match that it find in collectionValues array.

I remember I had another code that pass all tests except one another.

Maybe my question isn’t limited to how solve this challenge and I need to know more about how think as a developer, the brain algorithm of programmers when they face a task! Hmmm.

:heart:
:)‌

What does this do exactly? Does it check that the value for the current key is correct?

Yes and no :grimacing:. It doesn’t care about key, and look at all keys of current object.

You may add this after that line:

console.log(collectionValues, ".includes(", sourceValues[j],"); >>> returns ", ValueSearch);

Maybe I must limit checking scope to the pairs instead of whole object!

I think you might be overthinking the issue. You need to check that the value corresponding to the key is the same for both objects, right?

Solved :)‌ (At least in terms of passing tests)

function whatIsInAName(collection, source)
{
        let Output = [];

        let sourceKeys = Object.keys(source);
        let sourceValues = Object.values(source);

        for (let i = 0; i < collection.length; i++)
        {
                let collectionObjectKeys = Object.keys(collection[i]);
                let collectionObjectValues = Object.values(collection[i]);

                let Counter = 0;

                for (let j = 0; j < sourceKeys.length; j++)
                {
                        let SubCounter = 0;

                        for (let f = 0; f < collectionObjectKeys.length; f++)
                        {
                                let KeyComparator = String(collectionObjectKeys[f]).includes(sourceKeys[j]);
                                let ValueComparator = String(collectionObjectValues[f]).includes(sourceValues[j]);

                                if (KeyComparator === true && ValueComparator === true)
                                {
                                        SubCounter++;
                                }
                        }
    
                        if (SubCounter > 0)
                        {
                                Counter++;
                        }
                }

                if (Counter === sourceKeys.length)
                {
                        Output.push(collection[i]);
                }
        }

        return Output;
}

Thank you dear Jeremy.

1 Like