Need some help with delete Keyword to Remove Object Properties challenge

Need some help with delete Keyword to Remove Object Properties challenge
0

#1

I tried making a function to do just this, since I ran into similar problems. However, I keep getting a syntax error in the console, and it isn’t working to complete the challenge. I don’t see any syntax errors or anything that shouldn’t work. Anyone have any ideas what would make this work?
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/use-the-delete-keyword-to-remove-object-properties

let foods = {
  apples: 25,
  oranges: 32,
  plums: 28,
  bananas: 13,
  grapes: 35,
  strawberries: 27
};

// change code below this line
function Delete(arr, objName) {
  let newName = objName.replace(/"+/g, "");
  while (let i = 0; i < arr.length; i++) {
    delete newName[arr[i]];
  }
}

Delete(["oranges", "plums", "strawberries"], "foods");
// change code above this line

console.log(foods);

Use the delete Keyword to Remove Object Properties helpp
#2

I moved your question out of the other post, because you had a question about your on code instead of helping the OP of the other thread. In the future, just use the Ask for Help button on the challenge, so it brings in your formatted code along with the applicable challenge url.


#3

While loops only have a test condition, so you can not use let i = 0; or i++.

Also, newName is a string which has the value “foods”, so you are attempting to delete an undefined.

All you need to do is pass the foods object into the function, then iterate through arr and delete objName’s properties which are the same as the current value of arr[i].


#4

My mistake. I thought that I should post within the thread already about the particular challenge rather than make a new topic.

I made the corrections you suggested and was able to make the function work. Thanks for the pointers.


#5

When responding to another poster’s topic, it is best to stick with either making suggestions to help them or asking for clarification about something they may have written which you are confused about. The point is, the discussion should focus on their code and not your code, which is why we prefer a new topic for each user’s challenge questions. It keeps things more organized.