Wherefore art thou challenge - Intermediate Algorithm Scripting

feels like i’m missing something guys need a little bit of help

function whatIsInAName(collection, source) {

  var arr = [];

  // Only change code below this line

  let newArr=[];

  Object.values(source).forEach(value=>{

    newArr.push(value);

  })

  for(let i=0; i<collection.length;i++){

    for(let prop in collection[i]){

        if(newArr.indexOf(collection[i][prop])>=0){

            arr.push(collection[i]);

            break;

        }

    }

  }

  // Only change code above this line

 return arr.filter(object=>Object.keys(object).length>=newArr.length);

}

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

console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 }));

console.log(whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 }));

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

Hey. What problems are you having?

Hi Ariel, unfortunately my code doesn’t work properly i am having an issue while filtering the collection array

What exactly are you trying to achieve? What part isn’t working?

function whatIsInAName(collection, source) {

  var arr = [];

  // Only change code below this line

  let newArr=[];

  let arrKey=[];

  let count=0;

  for(let key in source){

    newArr.push(source[key]);

    arrKey.push(key);

  }

  for(let i=0; i<collection.length;i++){

    count=0;

    for(let prop in collection[i]){

        if(newArr.indexOf(collection[i][prop])>=0 && arrKey.indexOf(prop)>=0){

            count++;

            console.log(count);

            if(count>=newArr.length)

            {

              arr.push(collection[i]);

              console.log('\n');

              break;

            }

        }

    }

  }

  // Only change code above this line

 return arr;

}

i find the solutions even if it takes me almost 24 hours :smile: :smile: :sweat_smile: :sweat_smile:

Congratulations on figuring it out! Happy coding.

1 Like