Please break this line down further

I commented my question into the code below. I’m trying to understand how exactly entities[element] gets the value "&"

  **Your code so far**

function convertHTML(str) {
//separate string
const entities = { 
  "&": "&",
  "<": "&lt;",
  ">": "&gt;",
  "\"": "&quot;",
  "'": "&apos;",
let arr = str.split("").map(function(element) { 
  if(entities.hasOwnProperty(element)) { // if one of the entities above exists in the new string which we split, then we return the specific entities' second index?
    return entities[element];
  } else {
    return element;
//return arr.join("");

console.log(convertHTML('Stuff in "quotation marks"')); 
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Convert HTML Entities

Link to the challenge:

Split the string into an array characters

Make a new array from the charcter array

If the element (character) is in the special entities object

Then replace it

Otherwise use the character unaltered

This has many pieces, but it’s all array methods and an object used as a lookup table. The intermediate algorithm challenges are tricky, but they get much easier if you understand how objects work.

As is, this copied code won’t quite get you to the answer. You have an array but you should be returning a string. You need the commented out piece.

1 Like

Is one way to access the value of a name (or key) in a JavaScript object.

If you uncomment this the code it passes the tests, and returns something other than undefined.

1 Like

@JeremyLT @eoja thank you I took a break from coding so I mixed entities[element] with getting some at that index vs accessing object properties. It still feels strange working in functions and defining element like that.

Just curious, why wont entities.element work here?

There is nothing strange with defining a callback function inside of a map. That is how a map is supposed to work. Lots of solutions you’ve seen with map, filter, reduce, etc define a function, with various input variables, inside of the array method call.

This is the problem with trying to take apart someone else’s solutions. You inevitably end up using logic you don’t understand and often also syntax you don’t understand. And taking apart the solutions of others doesn’t really help you learn how to do logical problem solving on your own.

Case in point, object dot notation cannot work with a variable holding the name of the property that you want to access. It might be helpful for you to review basic syntax, especially for objects, arrays, and array methods.

I’m reviewing all JS content on W3schools and doing the exercises there before I do any more of these exercises. Considering I’ve stopped multiple times to go back and redo the curriculum content (without good results), I’m hoping this will be better.

W3Schools might work for you. I’m certainly not saying you must repeat lessons on freeCodeCamp, but clearly copying the work of others is not helping your understanding and ability to solve problems.

I promise you I’m not just copying solutions. If I did that I would’ve finished the curriculum 3 months ago.

On the way to finding what works for me, I have watched videos, read articles, etc so it’s just a process of finding something that clicks for me

Sure. Whatever you prefer to call reading the solutions or walkthroughs of others and replicating their logic and syntax without understanding, I recommend against doing that. It feels deceptively helpful, but can you honestly say that it has improved your ability to write solutions to complex problems without looking at the solutions of others?

Different things work for different people. Taking apart the code of others is an important skill, but it’s different from writing original solutions. If you want a job coding, you’ll need both skills. You don’t often get paid to replicate code where someone else has already solved the problem and provided code.

I like to code solutions to problems on hackerrank in JavaScript and Python. Once I figure it out I look in the discussions at both languages for solutions other people came up with. I learn a lot looking at other peoples’ solutions in terms of logic, and when I see something in their code I don’t understand I look it up and learn it which increases my knowledge . So, for me I find looking at “answers” or “other people’s code” is very beneficial. I will say come up with your own solution and you will learn, then look at others’ solutions and most likely you will learn more.

This is a good order to do this in. Make your own working solutions first and then look up other people’s code second. You get to practice both problem solving and reading code.

Yes that sounds ideal @eoja and thanks for that source.

I never copy someone’s code without attempting the problem on my own and doing all I can to solve it, but I have been victim of tendency to want to peek at the missing piece. Which is not ideal.

@JeremyLT I’m not looking to get a job as an engineer, but I want to be able to build products and services with code. I know it doesn’t change the fact that I should still be competent in coding. But you’re really active in the forum (which I appreciate) so I thought I’d mention that in case it ever matters on future questions I may post and so you know a little about my goals.

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