Wherefore art thou Help Needed

Wherefore art thou Help Needed
0.0 0

#1

Tell us what’s happening:
I was hoping someone can help me out. I can’t work out why my code isn’t working, have gone through it countless times and used debugger. I know it’s not the cleanest but to my mind should work. It passes all the test apart from the last one. Thanks in advance.

Your code so far

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var props = Object.keys(source);
  
  for (var i = 0; i < props.length; i++) {
  for (var j = 0; j < collection.length; j++) {
    if (!collection[j].hasOwnProperty(props[i]) || collection[j][props[i]] != source[props[i]]) {
      collection.splice(i, 1);
      if (collection.length > 0) {
      j --;
      }
    }
  }
}
  arr = collection;
  
  // Only change code above this line
  return arr;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:


#2

In the last test case, you get an infinite loop.


#3

Thanks for the response. In the end I realised I had made a silly mistake on the line:

collection.splice(i, 1)

where the i should be a j