Intermediate Algorithm Scripting - Wherefore art thou

Could someone give some insight as to why my return array doesn’t keep the quotes around the property values? What step in my code is removing them??

Disclaimer: I know my logic is flawed and using Object.keys makes more sense. And I also know with my current code it wont get passed the second check. But it’s bugging me that my return isn’t what I expect it to be.

My code so far:

function whatIsInAName(obj, tobj){
  let returnArr = [];
  let targetProp;
  let targetValue;
  for (let extract in tobj){
    targetProp = extract
    targetValue = tobj[extract]
  };
  for (let i in obj){
    for(let prop in obj[i]){
      if (obj[i].hasOwnProperty(prop) && prop == targetProp && obj[i][prop] == targetValue){
        returnArr.push(obj[i])
      }
    }
  }
  console.log(returnArr)
  /*
  for some reason this logs returnArr as:
  [ { apple: 1 }, { apple: 1 }, { apple: 1, bat: 2 } ]
  without any " "
  */
};

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Wherefore art thou

Link to the challenge:

Are you talking about the quotation marks around the property names (apple, bat)? You don’t technically need quotes around those names because there are no spaces in them. So your browser’s console is just choosing to show them without quotes.

Yeah I’m trying to get the result [{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }] but instead I’m getting the same thing but without any quotation marks. I’m doing this code within the console attached to the freeCodeCamp challenge and I’m assuming the quotes do matter because I still get a failure on test 2 which is:

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

should return

[{ "apple": 1 }, { "apple": 1 }, { "apple": 1, "bat": 2 }]"

I am assuming you are still using the same code you posted above. If your function would return returnArr instead of console.logging it, then your current code would pass tests 1, 2, and 4.

The quotations marks DO NOT matter.

:man_facepalming: …whoops, thank you. Got so caught up looking for some complex mistake that I forgot to return anything from the function at all.

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