Wherefore art thou vague requirements

Tell us what’s happening:

The task as phrased, with respect to expected outputs is vague. Thusly all but one test pass, and the way the question is phrased it seems that it should in fact fail.

Your code so far


function whatIsInAName(collection, source) {
  // What's in a name?
//console.log(JSON.stringify(source));
  var arr = [];
  const sourceString = JSON.stringify(source).replace("{","").replace("}",""); //This line is a workaround
  console.log(sourceString);
  // Only change code below this line
 /* collection.forEach(bj=> {
    console.dir(obj)
    console.log();
  })
  */(collection);
collection.forEach((obj, i) => {
 // console.log("I: " + i);
 const objString = JSON.stringify(obj);
  if(objString.indexOf(sourceString)>-1 
  || objString.indexOf(sourceString.split(":"))[1] > -1)
  arr.push(obj);
  
  })
  console.log(arr)
  // 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:

User Agent is: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/wherefore-art-thou/

In the test case below, neither of the two conditions in the if statement evaluate to true, so nothing is going to get pushed into arr.

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

Why? Because sourceString is “apple”:1,“cookie”:2 and objString is {“apple”:1,“bat”:2}, objString.indexOf(sourceString) evaluates to -1 AND objString.indexOf(sourceString.split(’:’))[1]) evaluates to undefined. Neither -1 or undefined is > -1, nothing gets pushed to arr.

Can you explain your algorithm to us and what you thought your if statement was going to do for you?

Hi,
Stringifying objects for comparison seems really difficult. Wouldn’t some test for matching key:value pairs between objects be easier to implement?

1 Like

That actually seems to be the better solution. I got it to work by doing that. Thank you. :slight_smile:)

1 Like

I was basically trying to compare key value pairs. It turns out I had an ID10T error. I ultimately indexed using Object.keys() and Object.values()