main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function whatIsInAName(collection, source) {
    // What's in a name?
    // Only change code below this line
    var arr = [];
    //Get the source key
    var keys = Object.keys(source);
    // Array for collection keys
    var colKey  = [];
    // Fill the collection keys into array
     for (var i=0;i<collection.length;i++)
      {
        colKey.push(Object.keys(collection[i]));
      }
    // store the lenth of collection keys once
    var colLen = colKey.length;
    // iterate over all collection keys one by one
    for (var j=0;j<colLen;j++)
      {
        // if and object has lenth > 1 then iterate over each object
        if (colKey[j].length > 1)
          {
            for (var k=0;k<colKey[j].length;k++)
              {
                // Check each object if it is equel to source key
                if(colKey[j][k] == keys)
                  {
                    // if equel then push it to arr
                    arr.push(collection[j]);
                  }
              }
          } 
        // if length is 1 then check for source key, if true push it in arr
        // This is never true (Why ?) in debug I have seen both have same values
        // So it must be true
        // While same kind of code at line 26 works.
            if (colKey[j] == keys)
              {
              arr.push(collection[j]);
          
              }
      }
    return arr;
  }
  
  
  whatIsInAName([{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }], { "a": 1});
  
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie