# Intermediate Algorithm : Wherefore art thou

``````function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
var collectionKeys = [];
for ( var i = 0; i < collection.length; i++){
collectionKeys.push(Object.keys(collection[i]));
}

var sourceKeys = Object.keys(source);

//for every key pair
for ( var t = 0; t < collectionKeys.length; t++){
console.log(collectionKeys[t]);
//for every key in key pair
for ( var x = 0; x < collectionKeys.length; x++){
console.log(collectionKeys[t][x]);
//see if key matches
for ( var y = 0; y < sourceKeys.length; y++){
if( sourceKeys[y] == collectionKeys[t][x]){
if( collection[t][collectionKeys[t][x]] == source[sourceKeys[y]]){
arr.push(collection[t].first+ " " + collection[t].last);
}else{
}
}else{
}
}
}
}

// Only change code above this line
return arr;
}

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

Hi,

so I removed the console.log() statements, because they confused me about what was the actual return value of the function and they are not required in the prompt.

Then I was left with this:

``````function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
var collectionKeys = [];
for (var i = 0; i < collection.length; i++) {
collectionKeys.push(Object.keys(collection[i]));
}

var sourceKeys = Object.keys(source);

//for every key pair
for (var t = 0; t < collectionKeys.length; t++) {

//for every key in key pair
for (var x = 0; x < collectionKeys.length; x++) {
//see if key matches
for (var y = 0; y < sourceKeys.length; y++) {
if (sourceKeys[y] == collectionKeys[t][x]) {
if (collection[t][collectionKeys[t][x]] == source[sourceKeys[y]]) {
arr.push(collection[t].first + " " + collection[t].last);
}
}
}
}
}

// Only change code above this line
return arr;
}
``````

For the first test the return value is:

``````[ 'Tybalt Capulet' ]
``````

A string in an array, but according to the test it should be:

``````[{ first: "Tybalt", last: "Capulet" }]
``````

An array containing an object and two key-value-pairs. So that is one issue. Also all the other tests ask for arrays with objects and various key-value-pairs.

On trying the second test the return value is:

``````[ 'undefined undefined',
'undefined undefined',
'undefined undefined' ]
``````

Desired:

``````[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]
``````

You hardcoded “first” and “last” to be pushed to arr. But except for the first test case, none have “first” and “last” keys. So that has to become more flexible …

``````arr.push(collection[t].first + " " + collection[t].last);
``````

I am not sure that is all, but these do need some work. Please feel free to ask more questions, if needed.

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

1 Like

Thanks for the reply and great explanation. and how should I fix it. I am so confused . Those console log I kept for my explanation by mistakenly copied and pasted the code here.

Welcome. Here is a suggestion in sudo code:

1 Like

//i fixed it

function checkObjects(first, source) {
var sourceKeys = Object.keys(source);
for (var i = 0; i < sourceKeys.length; i++) {
var key = sourceKeys[i];
if (!(key in first)) {
return false;
}

``````if (source[key] !== first[key]) {
return false;
}
``````

}

return true;
}

function whatIsInAName(collection, source) {
var arr = ;
// Only change code below this line
for (var i = 0 ; i < collection.length; i++) {
if(checkObjects(collection[i], source)) {
arr.push(collection[i]);
}
}

// Only change code above this line
return arr;
}

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

1 Like

Cool, congratulations!