Who wants to see some funky code?

I got tired of the “Wherefore art thou” challenge, so I solved it… in a funky way.

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var answer = [];
  arr = arr.concat(collection);
  var matchKeys = Object.keys(source);
  var matchValues = Object.values(source);
  for (i=0; i<arr.length; i++) {
    var truth = 0;
    for (j=0; j<Object.keys(arr[i]).length; j++) {
      for (k=0; k<matchValues.length; k++) {
        if (Object.keys(arr[i])[j] == matchKeys[k]) {
          truth += 1;
          if (truth == matchValues.length) {
            if (Object.values(arr[i])[j] == matchValues[k]) {
              answer.push(arr[i]);
            }
          }
        }
      }
    }
  }
  // Only change code above this line
  return answer;
}

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

Yeah, it’s a little ugly. If you properly indent, it’s a little better. And I like to label close brackets if they’re far from their open, like this:

function whatIsInAName(collection, source) {
  // What's in a name?
  var arr = [];
  // Only change code below this line
  var answer = [];
  arr = arr.concat(collection);
  var matchKeys = Object.keys(source);
  var matchValues = Object.values(source);
  for (i=0; i<arr.length; i++) {
    var truth = 0;
    for (j=0; j<Object.keys(arr[i]).length; j++) {
      for (k=0; k<matchValues.length; k++) {
        if (Object.keys(arr[i])[j] == matchKeys[k]) {
          truth += 1;
          if (truth == matchValues.length) {
            if (Object.values(arr[i])[j] == matchValues[k]) {
              answer.push(arr[i]);
            }
          } // if (truth...
        } // if (Object.keys...
      } // for k
    } // for j
  } // for i
  // Only change code above this line
  return answer;
}
1 Like

Play that funky code, white boy.

1 Like

Oh, I was referring to the code itself. The indentation didn’t paste over into this forum. Were you able to achieve this by using a blockquote?

You can get code blocks to show correctly by putting three backticks (key below the ESC) on the line before and three on the line after.

I’ve also been labelling closing brackets. I find the whole brackets thing to be the most annoying thing about programming so far. Labelled brackets help immensely by saving you a lot of time.