Convert HTML entities - for in loop not working

Hi there -

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!

function convertHTML(str) {
  const regExp = /[&<>"']+/g;
  const listOfChar = {
    a:{
      'symbolChar': '&',
      'entity': '&amp;'
    },
    b:{
      'symbolChar': '<',
      'entity': '&lt;'
    },
    c:{
      'symbolChar': '>',
      'entity': '&gt;'
    },
    d:{
      'symbolChar': '"',
      'entity': '&quot;'
    },
    e:{
      'symbolChar': "'",
      'entity': "&apos;"
    }
};

while (regExp.test(str)){ 
  for(const item in listOfChar){
    return str.replaceAll(listOfChar[item]['symbolChar'], listOfChar[item]['entity']);
  }
}
return str;
 
}

convertHTML("Hamburgers < Pizza < Tacos < Tacos");

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.

1 Like

A few thoughts,

  1. 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 &amp; directly…

  1. Have you considered a map?

Side note: I edited your post to include the challenge link.

It is always best to include all of the information when asking for help. :grinning:

2 Likes

it doesn’t. I replaced while for if and got the same result.

Other bugs are masking it, but @ArielLeslie is right about that issue as well.

1 Like

I think you can use replace here if you want. The key is to remember that replace can take a function as the second arg instead of a string.

1 Like

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”.

the loose return in here is masking your other bugs

1 Like
  1. 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.

  2. truthfully, i tried something like

let obj = {
'&' : '&amp;'
'<': '&lt;'
}

This seemed more appropriate, but I was still having issues.

  1. I have not considered map(), but I will give that a try.

Thanks so much for your feedback

thanks. I re-read your note and it does make sense. It didn’t give me an infinite loop, but you brought up a great point. Thanks!

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.

1 Like

Thanks. It seemed to me that way too.
I will also try using a function as the second argument.
I have to keep trying. Thanks much!

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.

2 Likes

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.

3 Likes

Thanks so much. I really appreciate the help!!!

Much appreciated. Thank you!

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