Wherefore art thou Algorithm

Wherefore art thou Algorithm
0

#1

Hello, i’ve been stuck on the Wherefore art thou algorithm challenge for a while, my code currently passes the first two tests.

i figure i need to modify the condtion somehow to pass the tests where source contains more than one object, i’m struggling to think how to approach it , can anyone give me a hint or suggestion?

Thanks


function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var prop = null;
  // for source prop loop
  var objectKeys = Object.keys(collection);
  // get array of non negative integers to loop over
  for (var i = 0; i < objectKeys.length; i++) {
   var obj =  collection[objectKeys[i]];
   // get object from collection
    for (prop in source) {
      if (obj.hasOwnProperty(prop) && source[prop] === obj[prop]) {
        // if object contains source property & values are equal
        arr.push(obj);
        // push whole object to array
        // doesn't pass 3 & 4th tests yet
        // pushing objects where it has at least one of the source props atm
      }
    }
  }
  return arr;
  // only change code above this line
}


#2

You’re really close! You just need to somehow check that all of the properties in source are in obj as well. Think about how to solve that problem, then apply the solution to your current function.


#3

hey there, thanks for the reply, i gathered that was the problem with the current condtion, it’s nice to have that thought confirmed by someone else.

i’ve been thinking it through and tryed a few more things but i’m still unsure how to approach that paticular problem, is their anything else you could suggest?


#4

I’ve been having the exact same problem (code passed the first two tests, but fails the others, when there are two properties in source). I tried wrapping my code in an additional for loop to iterate through each item in source, but now I only get an empty array and pass zero tests. Any hints would be greatly appreciated!

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
 
  // Only change code below this line
  for (var j=0; j<source.length; j++) {
    var arrKey = Object.keys(source[j]);  
    //var arrKey = Object.keys(source);  original line 8, before addt'l for loop added
    var key = arrKey.toString();
      for (var i=0; i<collection.length; i++){
        if (source[key] === collection[i][key]) {
          arr.push(collection[i]);
        }
      }
 }
  // Only change code above this line
  return arr;
}

#5

I am having the same issue as you… Were you able to solve??? I am able to complete the fist two but where there are multiple key:value pairs I am unable to solve… Can some one provide some insight??

` function whatIsInAName(collection, source) {
// What’s in a name?
var arr = [];
var arr2 = [];
// Only change code below this line
/* for (var i = 0; i < collection.length; i++)
{
for (var j in collection[i]) {
if (collection[i][j] === source[j]){
arr.push(collection[i]);
}
}

} */

/* For each value in the collection, you need to check that each key in Object.keys is in the that value from collection and has the same
value as source */

for (var k = 0; k < collection.length; k++ ) {
var keys = Object.keys(source);

if ( keys[k] === collection[k] ) {
arr[k] = keys[k];
}
}

return arr;
}

whatIsInAName([{ “a”: 1, “b”: 2 }, { “a”: 1 }, { “a”: 1, “b”: 2, “c”: 2 }], { “a”: 1, “b”: 2 });

`

#6

i’ve long solved it, i actually finished the last intermediate algorithm challenge a couple weeks ago.

i tried to close this as soon as i did, but you don’t appear to able to do that.

from logging your output it appears you are sometimes pushing duplicate objects to the final arr.

this is because with your current code, if several props match, collection[i] will be pushed each time they match. Also, if there is only one match, the object will still be pushed. Look at your if condition and think why this may be the case.

my advice, also is think of how you can keep track of the total number of matches, so you only push objects when they are source prop number of matches. You also might wanna consider looping through the source props instead of collection[i], you can still get what you want from comparing those props to collection[i][j]. and that way it will only do the check source prop number of times.

hope that’s helpful.

if it isn’t, i’d recommend asking on chat or opening up a new topic here, this topics kinda dead haha.