No errors in vscode but code won't pass

My code returns correctly in vscode but I’m getting a reference error in the fCC console:

ReferenceError: assignment to undeclared variable property

I can’t spot the undeclared variable and would appreciate any help.

An explanation as to the discrepancy between the consoles and any tips to debug similar would be extra appreciated!

I’ve included an extra code block at the bottom that console logs the various fCC tests fro you convenience :slight_smile:

  **Your code so far**

function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line
collection.forEach(element => {
  let allHere = true;
  for(property in source){
    if(source[property] != element[property])
      allHere = false;
  }
  if(allHere)
    arr.push(element);
});
// Only change code above this line
return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
  **Optional consolelogging:**
  let test1 = whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
  let test2 = whatIsInAName([{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }], { "apple": 1 });
  let test3 = whatIsInAName([{ "apple": 1, "bat": 2 }, { "bat": 2 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "bat": 2 });
  let test4 = whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }], { "apple": 1, "cookie": 2 });
  let test5 = whatIsInAName([{ "apple": 1, "bat": 2 }, { "apple": 1 }, { "apple": 1, "bat": 2, "cookie": 2 }, { "bat":2 }], { "apple": 1, "bat": 2 });
  let test6 = whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3});

  console.log(`Test 1:`, test1);
  console.log(`Test 2:`, test2);
  console.log(`Test 3:`, test3);
  console.log(`Test 4:`, test4);
  console.log(`Test 5:`, test5);
  console.log(`Test 6:`, test6);

Challenge: Wherefore art thou

Link to the challenge:

This is telling you exactly what the issue is with your code. You can’t use a variable without declaring it first. Declare the property variable properly and it will work.

So why don’t I get an error in a different console?
Is variable declaration somehow treated differently?

Perhaps it isn’t running JS in strict mode? In strict mode all variables must be declared with either var, let, or const. Strict mode is a good thing, you want to use it. I’m sure the FCC tests all run in strict mode.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.