'Wherefore art thou' problem

'Wherefore art thou' problem
0.0 0

#1

So this is my code below for this challenge. It passes every test except this one:
whatIsInAName([{ “a”: 1, “b”: 2 }, { “a”: 1 }, { “a”: 1, “b”: 2, “c”: 2 }], { “a”: 1, “c”: 2 }) should return [{ “a”: 1, “b”: 2, “c”: 2 }]. It returns an extra {“a”:1} in my answer. I am convinced something is wrong with the site at the moment because whenever I try to return the statement inside the last if, it prints false, which means that this extra {“a”: 1} should not be printed.

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

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Nothing is wrong with the site. It is your solution’s logic.

Inside the 1st iteration of the outer for loop, i = 0.

In the first iteration of the inner for loop j = 0 and i = 0, collection[0] is {a: 1, b: 2} and sourceKeys[0] is ‘a’, so the first if statement is true because ‘a’ is a property of {a: 1, b: 2} . In the nested if statement condition, you check if collection[0][sourceKeys[0]] is equal to source[sourceKeys[i]]. Since sourceKeys[0] is ’ a’ you are checking collection[0][‘a’] (which is 1) is equal to source[‘a’] (which is 1). They are equal, so you increment count by 1. Now count is equal to 1. Then you check if (count == sourceKeys.length) { which is false, so nothing is added to arr.

Inside the 2nd iteration of the inner for loop j=0 and i = 1, collection[0] is {a: 1, b: 2} and sourceKeys[1] is ‘c’, so the first if statement is false because ‘c’ is not a property of {a: 1, b: 2} . The count variable is still equal to 1. Then you check if (count == sourceKeys.length) { which is false, so nothing is added to arr.

Inside the 2nd iteration of the the outer for loop, j = 1.

In the first iteration of the inner for loop j = 1 and i = 0, collection[1] is {a: 1} and sourceKeys[0] is ‘a’, so the first if statement is true because ‘a’ is a property of {a: 1} . In the nested if statement condition, you check if collection[1][sourceKeys[0]] is equal to source[sourceKeys[i]]. Since sourceKeys[0] is ’ a’ you are checking collection[1][‘a’] (which is 1) is equal to source[‘a’] (which is 1). They are equal, so you increment count by 1. Now count is equal to 2. Then you check if (count == sourceKeys.length) { which is true, because count is 2 and sourceKeys.length is 2, so {a: 1} gets added to arr.

That is how you end up with {a: 1} in the final array.


#4

omg my bad, put count = 0 in the wrong place. Thank you!