# freeCodeCamp Challenge Guide: Wherefore art thou

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 11:36pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092 "2016-06-30T23:36:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 11:36pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/1 "2016-06-30T23:36:15Z")

</div>

# [Wherefore Art Thou](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou)

* * *

## Problem Explanation

Write an algorithm that will take an `array` for the first argument and return an `array` with all the `object`s that matches all the properties and values in the `Object` passed as second parameter.

#### Relevant Links

- [For Loops](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
- [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
- [Object.hasOwnProperty()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
- [Object.keys()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys)

* * *

## Hints

### Hint 1

You may use `for` loop or the `filter()` method.

### Hint 2

Consider using the `hasOwnProperty()` method to know if the property name exists in an object (as its own property).

### Hint 3

Check if the value of the property in a `collection` object`matches the values associated with the keys of the`source` object.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function whatIsInAName(collection, source) {
> // What's in a name?
> const collectionMatches = [];
> 
> for (let i = 0; i < collection.length; i++) {
> let foundMismatch = false;
> 
> for (const sourceProp in source) {
> if (collection[i][sourceProp] !== source[sourceProp]) {
> foundMismatch = true;
> }
> }
> if (!foundMismatch) {
> collectionMatches.push(collection[i]);
> }
> }
> return collectionMatches;
> }
> 
> ```
> 
> #### Code Explanation
> 
> - We use a `for` loop to iterate over every object in the `collection`.
> - We check for a mismatching value between the `source`s keys and the current object.
> - If no mismatch is found, the current object is added to the array of `collectionMatches`.

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> function whatIsInAName(collection, source) {
> // "What's in a name? that which we call a rose
> // By any other name would smell as sweet.”
> // -- by William Shakespeare, Romeo and Juliet
> const souceKeys = Object.keys(source);
> 
> // filter the collection
> return collection.filter(obj => {
> for (let i = 0; i < sourceKeys.length; i++) {
> if (obj[sourceKeys[i]] !== source[sourceKeys[i]]) {
> return false;
> }
> }
> return true;
> });
> }
> 
> ```
> 
> #### Code Explanation
> 
> - We filter through the array using `.filter()`.
> - Using a `for`loop we iterate through each item in the object.
> - We use a `if` statement to check if the value of the current property for the object matches the value in source.
> - We return `false` if any mismatch is found. Otherwise, we return `true`;

> **Solution 3 (Click to Show/Hide)**
>
> ```javascript
> function whatIsInAName(collection, source) {
> // "What's in a name? that which we call a rose
> // By any other name would smell as sweet.”
> // -- by William Shakespeare, Romeo and Juliet
> const sourceKeys = Object.keys(source);
> 
> return collection
> .filter(obj => sourceKeys
> .every(key => obj[key] === source[key]));
> }
> 
> ```
> 
> #### Code Explanation
> 
> - We filter through the collection using `.filter()`.
> - We return a `Boolean` value for the `.filter()` method by checkif if `.every()` source key value matches the current object’s value.

---

<div class="post-metadata">

**Author:** ![sautille](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sautille/32/156325_2.png) [@sautille](https://forum.freecodecamp.org/u/sautille)\
**Post date:** [August 2, 2016, 1:07pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/2 "2016-08-02T13:07:31Z")

</div>

There is a bug in the ‘advanced’ solution above. I made a PR to the old `wiki` project, but was told to contribute here instead. Would be great if the ‘advanced’ solution could be updated. Details can be found here: [https://github.com/FreeCodeCamp/wiki/pull/1246](https://github.com/FreeCodeCamp/wiki/pull/1246)

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [August 2, 2016, 1:33pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/4 "2016-08-02T13:33:34Z")

</div>

@sautille is still a ‘new user’, once enough trust/reputation is built to progress to ‘basic user’ people can edit wiki posts.

From memory you just need to read a few posts and interact a little to ‘level up’.

**Edit**

> **[What do user trust levels do?](https://meta.discourse.org/t/what-do-user-trust-levels-do/4924/4)**
>
> Basic (1) If a user sticks around long enough to read a bit, Discourse now trusts them as a basic user. They can get to trust level 1 by… entering at least 5 topics reading at least 30 posts spend a total of 10 minutes reading posts Admins can...

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 3, 2016, 11:18pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/6 "2016-08-03T23:18:18Z")

</div>

This is just a basic mechanism to avoid spaming.

---

<div class="post-metadata">

**Author:** ![raisedadead](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/raisedadead/32/244209_2.png) [@raisedadead](https://forum.freecodecamp.org/u/raisedadead)\
**Post date:** [August 4, 2016, 8:02pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/7 "2016-08-04T20:02:53Z")

</div>

@sautille: I have updated the changes to match and reflect the pull request you made earlier and the change on the main repository as well.

Thanks for contributing.

---

<div class="post-metadata">

**Author:** ![nazimkazim](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nazimkazim/32/177547_2.png) [@nazimkazim](https://forum.freecodecamp.org/u/nazimkazim)\
**Post date:** [August 8, 2016, 7:19am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/8 "2016-08-08T07:19:46Z")

</div>

Great post i apprecialte your efforts!

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 10, 2016, 2:43am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/9 "2016-08-10T02:43:30Z")

</div>



---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 20, 2016, 3:39am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/10 "2016-08-20T03:39:10Z")

</div>



---

<div class="post-metadata">

**Author:** ![okristian1](https://avatars.discourse-cdn.com/v4/letter/o/df705f/32.png) [@okristian1](https://forum.freecodecamp.org/u/okristian1)\
**Post date:** [January 13, 2017, 1:16pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/11 "2017-01-13T13:16:27Z")

</div>

I’ve tried to solve this and gotten 2/4 green marks but gave up and came here to take a peek.

But I only got more confused. The exersize says to only change code between the lines.

```
 var arr = [];
  // Only change code below this line
  
  
  // Only change code above this line
  return arr;

```

But all the solutions posted here ignore that.

---

<div class="post-metadata">

**Author:** ![jacobworrel](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacobworrel/32/163444_2.png) [@jacobworrel](https://forum.freecodecamp.org/u/jacobworrel)\
**Post date:** [February 12, 2017, 8:26am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/12 "2017-02-12T08:26:07Z")

</div>

I also got 2/4 green marks using the code below… I understand why my code doesn’t work but I was wondering if anyone knows if it’s possible to make this approach work (without using the filter() method) considering that the exercise tells us not to change the code between the lines. Cheers!

```
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  for (var i = 0; i < collection.length; i++) {
    for (var prop in source) {
      if (collection[i].hasOwnProperty(prop) && collection[i][prop] === source[prop]) {
        arr.push(collection[i]);
      }
    }
  }
  // Only change code above this line
  return arr;
}

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

---

<div class="post-metadata">

**Author:** ![macloo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/macloo/32/162907_2.png) [@macloo](https://forum.freecodecamp.org/u/macloo)\
**Post date:** [February 16, 2017, 11:13pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/13 "2017-02-16T23:13:50Z")

</div>

@jacobworrel I think your code is pushing to arr when only ONE property is matched. However, sometimes the source has more than one, an all must be matched.

---

<div class="post-metadata">

**Author:** ![jacobworrel](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacobworrel/32/163444_2.png) [@jacobworrel](https://forum.freecodecamp.org/u/jacobworrel)\
**Post date:** [February 17, 2017, 6:56pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/14 "2017-02-17T18:56:13Z")

</div>

Right.I just wonder if there’s any way to make my code work when there is more than one property…

---

<div class="post-metadata">

**Author:** ![beansprout](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/beansprout/32/21017_2.png) [@beansprout](https://forum.freecodecamp.org/u/beansprout)\
**Post date:** [March 11, 2017, 3:41pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/15 "2017-03-11T15:41:00Z")

</div>

**Help! Can anyone tell me why my code removed the `""` from the keys in my solution?**  
Here’s my code:

```auto
function whatIsInAName(collection, source) {
	var arr=[];
	var k = Object.getOwnPropertyNames(source); 
	var v = source[k]; // assigns var to value in key value pair

	//loop through array of objects
	 for (var i = 0; i < collection.length; i++) {

		//check each object for matching key
	 	if (collection[i].hasOwnProperty(k)) {
	 		
			if (collection[i][k] == v ) { // if object has a match of key value pair...
				arr.push(collection[i]); // add object to arr
			}
		}
	 }
	return arr;
}

```

**The code passed for the 1st test:**

`whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });`

but the 2nd didnt pass (keys aren’t in double quotes).

**2nd test:**  
`whatIsInAName([{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }], { "a": 1 })`

my function returned this:

`[{ a: 1 }, { a: 1 }, { a: 1, b: 2 }]`

instead of this:  
`[{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }]`

**Why were my keys changed when I pushed them to arr?**

And why do none of the spoiler solutions use the starter code that was there? (where the last line is `return arr`?)

---

<div class="post-metadata">

**Author:** ![JP-Dub](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jp-dub/32/23213_2.png) [@JP-Dub](https://forum.freecodecamp.org/u/JP-Dub)\
**Post date:** [March 15, 2017, 9:59pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/16 "2017-03-15T21:59:24Z")

</div>

Is it the answer they were looking for? Probably not, given the replies, but I found a solution that works and just thought I would pass it on. Cheers!

```
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  
var name,
    key,
    soVal,
    coVal;
for(name in source){
  soVal = source[name];
} 
    var i=0;
      for(i; i< collection.length; i++) {
        var obj = collection[i];
          for(key in obj){
            coVal = obj[key];
            if (coVal == soVal && name == key) {
            arr.push(obj);
            }
          } 
      }
     
  // Only change code above this line
  return arr;
}
```

---

<div class="post-metadata">

**Author:** ![seestur](https://avatars.discourse-cdn.com/v4/letter/s/cdc98d/32.png) [@seestur](https://forum.freecodecamp.org/u/seestur)\
**Post date:** [March 18, 2017, 12:25am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/17 "2017-03-18T00:25:52Z")

</div>

FYI, the code above for the advanced solution is corrected, but the repl.it it links to still has the old code.

---

<div class="post-metadata">

**Author:** ![TM418](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tm418/32/70072_2.png) [@TM418](https://forum.freecodecamp.org/u/TM418)\
**Post date:** [April 1, 2017, 3:02am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/18 "2017-04-01T03:02:38Z")

</div>

do we have to use the methods in helpful links?

the filter() method works as well

 ![](https://global.discourse-cdn.com/freecodecamp/original/3X/0/9/09adca64229f1b0e008a34f323c5829829792936.png)

---

<div class="post-metadata">

**Author:** ![BMars](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bmars/32/126079_2.png) [@BMars](https://forum.freecodecamp.org/u/BMars)\
**Post date:** [April 4, 2017, 3:20am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/19 "2017-04-04T03:20:27Z")

</div>

This solution worked for me and it shares your approach. It passed all the test cases.

```
function whatIsInAName(collection, source) {
  var arr = [];
for (var keyd in source){
 }for (var key in collection){
     if (source[keyd] === collection[key][keyd]){
          arr.push(collection[key]);}
}return(arr);}
```

---

<div class="post-metadata">

**Author:** ![cvdeveloper13](https://avatars.discourse-cdn.com/v4/letter/c/bc8723/32.png) [@cvdeveloper13](https://forum.freecodecamp.org/u/cvdeveloper13)\
**Post date:** [April 14, 2017, 7:07am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/20 "2017-04-14T07:07:49Z")

</div>

I solved this with a comparing the objects.  
Here is the code:

```auto
function whatIsInAName(collection, source) {
  // What's in a name?  
  return collection
    .filter((obj) => JSON.stringify(Object.assign(JSON.parse(JSON.stringify(obj)), source)) === JSON.stringify(obj));
}

```

Does comparing the objects works faster or slower relatively to using the loop with comparing properties. (Sry for bad English 🙂 )

---

<div class="post-metadata">

**Author:** ![IrinaDorofeeva](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/irinadorofeeva/32/33813_2.png) [@IrinaDorofeeva](https://forum.freecodecamp.org/u/IrinaDorofeeva)\
**Post date:** [April 17, 2017, 4:35am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/21 "2017-04-17T04:35:56Z")

</div>

I solved that with couple for loops, it is not elegant as some above solutions, but it works:

```
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  
  arr = Array.prototype.slice.call(arguments);
  // Only change code below this line
  
  
  var key = Object.keys(source);
  var returncollection =[];
 
  for (i=0; i<collection.length; i++)
    {     
   for (j=0; j<key.length; j++)
    {
     if (collection[i].length<key.length)
       {
        break; 
       } 
     if (collection[i][key[j]]!==source[key[j]])
       {
        break; 
       }
      if (j==key.length-1)
       {
        returncollection = returncollection.concat(collection[i]);
       }
     }      
    }
    //collection2 = collection2.concat(collection[i]);     
  
  // Only change code above this line Object.keys(collection[i])[j]
  return returncollection;
}
```

---

<div class="post-metadata">

**Author:** ![br3ntor](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/br3ntor/32/7392_2.png) [@br3ntor](https://forum.freecodecamp.org/u/br3ntor)\
**Post date:** [May 30, 2017, 5:32am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092/22 "2017-05-30T05:32:03Z")

</div>

Some really interesting solutions in here.

Here is my solution that turned out pretty close to the basic spoiler. I even ended up using some same variable names by coincidence.

I couldn’t figure out an elegant solution in the if/else statement like the basic solution spoiler does on this line:  
`if(!obj.hasOwnProperty(srcKeys[i]) || obj[srcKeys[i]] !== source[srcKeys[i]])`  
but I figured out an interesting way to get my return to be true if the tests were passed.

```auto
function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line  

  arr = collection.filter(function(obj) {
    var srcKeys = Object.keys(source); 
    var test = 0;
    for (var i = 0; i < srcKeys.length; i++) {
      if (obj.hasOwnProperty(srcKeys[i]) && obj[srcKeys[i]] === source[srcKeys[i]]) {
        test += 1;
      } else {
        test += -1;
      }
    }
    return test === srcKeys.length;
  });
  
  // Only change code above this line
  return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

```

[Next page](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-wherefore-art-thou/16092.md?page=2)
