What's in a name (almost got it )

Hi,
I almost have this solved, it returns the right results, but maybe of the wrong type ? I hope someone can help :slight_smile:
Thanks

Your code so far


function whatIsInAName(collection, source) {
 // What's in a name?
 var arr = [];
 let hasProp="";
 let eachKey = [];
 let count = 0;
	for(let index in collection){
		for(let key in source){
	                hasProp = collection[index].hasOwnProperty(key);   			
		        if(hasProp){
				if(collection[index][key] === source[key]){
				   eachKey.push(key);
				}
		        }
			++count;
		}
		if(eachKey.length===count){
		    Object.entries(collection[index]).forEach(([key, value])=>{     			
		        arr.push(`${key}: ${value}`);             
	            });
		}
		eachKey.splice(0,eachKey.length);  
		count=0;
   }
 return arr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0.

Challenge: Wherefore art thou

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou

Hi @torentrap,

For the second test you are returning [ 'apple: 1', 'apple: 1', 'apple: 1', 'bat: 2' ] while you should be returning [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]

I believe some of the other tests fail similarly. Hope that’s helpful.

-4trio19 :pineapple:

Thanks for the reply. I changed part of the code, to this:

if(eachKey.length===count){
			arr.push("{");
		       Object.entries(collection[index]).forEach(([key, value])=>{     			
		                      //arr.push(`${key}:${value}`);  
                                     arr.push("\"" + key + "\"" + ":" + value);
                        });
			arr.push("}");
}

But still it’s not correct it seems, even though the returned values are the same.
It’s still some formatting problem?

I’m guessing it’s the commas addes with each push() , or maybe because first and last are surrounded with quotes ,while they shouldn’t be ?

Hope someone can help :slight_smile:

Solved it at last.
In case anyone wants to know, I needed to change these lines :

if(eachKey.length===count){
          arr.push(collection[index]);
}

:slight_smile:

1 Like

Nice work @torentrap !! :smile:

Thank you :grinning: