I’m having a hard time with the “HTML” entities challenge. I decided to create an object and then just the for in loop to iterate over the object and replace the symbol for the html entity, but it is not working. Can you offer any guidance? What am I not seeing? As always, thanks for your help!
Does that while create an infinite loop? If you keep checking of & after doing your replacement, you will always find one because & is used in the entity.
Repeatedly replacing symbols can really screw you up here. A regex replace isn’t a great approach. For example, what happens when you replace & after some other items are processed?
Why are you going through the extra work to hide the true key-value pair relationship here? & maps to & directly…
I think you’re saved from the infinite loop because you’re not actually putting in the entitys. Because of the way you chose to structure your object, when I run your code what I’m actually seeing is the string being turned into “abc”.
yes, it can get messy. I thought replace() was the best approach for it. It isn’t, it is? I decided replaceAll() to make sure it replaces each symbol if there were more than 1. It would pass some of the tests but not all.
Also, I was wondering if perhaps my regExp was not accurate.
truthfully, i tried something like
let obj = {
'&' : '&'
'<': '<'
}
This seemed more appropriate, but I was still having issues.
I have not considered map(), but I will give that a try.
It is a perfectly acceptable approach to this solution and to be honest would probably be my first choice for solving this. The key is that you have to use a function as the second parameter.
I’d agree that a replace is fine. My objection is to a replaceAll in a loop where you don’t control the order. You can make bad output easily that way.
Map is more natural to me, but I think a map approach and a replace with a replacer function approach do basically the same thing at a low level?
I’d still remove the outer while loop. It makes an infinite loop for any input with a & once you remove the stray return.
Yep. @veronicarbulu I’ll give you a hint. If you use the replace method then you do not need any loops at all because replace does the “looping” for you.