# Convert HTML Entities-problem in a for Loop

**URL:** <https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [November 9, 2017, 11:38am UTC](https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194 "2017-11-09T11:38:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lisistrati](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lisistrati/32/67338_2.png) [@lisistrati](https://forum.freecodecamp.org/u/lisistrati)\
**Post date:** [November 9, 2017, 11:38am UTC](https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194/1 "2017-11-09T11:38:18Z")

</div>

**Tell us what’s happening:**  
hello everybody, im trying to figure out what is the problem there as i use this switch -case in a for Loop and it works for all the tests except this one ‘\<\>’. It seems it makes the first replacement right the second also right but it can not keep both changes till the end. It is like for every new change it uses again the initial str and not the newStr. Is there any ideas how i can fix this?

**Your code so far**

```auto
function convertHTML(str) {
  // &colon;&rpar;
   var newStr=str;
  var spaceRemover = str.split(' ').join('');

   for(i=0; i<str.length; i++){
     
  switch (true) {
     
    case str[i].includes('&'):
       newStr=spaceRemover.replace(/&/g, ' &amp; ');
      break;
    case str[i].includes('<'):
      newStr=spaceRemover.replace(/</g, ' &lt; ');
      break;
      
     case str[i].includes('>'):
      newStr=spaceRemover.replace(/>/g, ' &gt; ');
      break;
     case str[i].includes('"'):
      newStr=newStr.replace(/"/g, '&quot;');
      break;
      case str[i].includes("'"):
      newStr=newStr.replace(/'/g, '&apos;');
      break;
   
  }

 }
console.log(newStr);
 return newStr;
}
convertHTML("<>");

```

**Your browser information:**

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36`.

**Link to the challenge:**  
[https://www.freecodecamp.org/challenges/convert-html-entities](https://www.freecodecamp.org/challenges/convert-html-entities)

---

<div class="post-metadata">

**Author:** ![lisistrati](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lisistrati/32/67338_2.png) [@lisistrati](https://forum.freecodecamp.org/u/lisistrati)\
**Post date:** [November 9, 2017, 12:36pm UTC](https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194/2 "2017-11-09T12:36:12Z")

</div>

i finaly found the solution i use arrays instead of strings with the same logic!

---

<div class="post-metadata">

**Author:** ![Elen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/elen/32/70607_2.png) [@Elen](https://forum.freecodecamp.org/u/Elen)\
**Post date:** [November 9, 2017, 3:49pm UTC](https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194/3 "2017-11-09T15:49:28Z")

</div>

Hello! And why use loop at all? The method .replace () itself searches in what place the necessary element is located and changes it. I did this:

```auto
function convertHTML(str) {
  str = str.replace(/&/g, '&amp;');
  str = str.replace(/</g, '&lt;');
  str = str.replace(/>/g, '&gt;');
  str = str.replace(/"/g, '&quot;');
  str = str.replace(/'/g, '&apos;');
    
  return str;
}

```

Can anyone know the best solution? Share, please 🙂

---

<div class="post-metadata">

**Author:** ![Elen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/elen/32/70607_2.png) [@Elen](https://forum.freecodecamp.org/u/Elen)\
**Post date:** [November 9, 2017, 4:45pm UTC](https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194/6 "2017-11-09T16:45:16Z")

</div>

Thank you! Very laconic and elegant! Only I did not understand how this part of the code works:  
find =\> conversions [find]  
Explain, please, if it is not difficult.

---

<div class="post-metadata">

**Author:** ![Elen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/elen/32/70607_2.png) [@Elen](https://forum.freecodecamp.org/u/Elen)\
**Post date:** [November 9, 2017, 5:30pm UTC](https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194/8 "2017-11-09T17:30:54Z")

</div>

Oh, so that’s clear. Thank you 🙂

---

<div class="post-metadata">

**Author:** ![vmubashir809](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vmubashir809/32/78269_2.png) [@vmubashir809](https://forum.freecodecamp.org/u/vmubashir809)\
**Post date:** [February 16, 2018, 5:04pm UTC](https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194/9 "2018-02-16T17:04:44Z")

</div>

**i am used this code**

function convertHTML(str) {  
var arr=[];  
arr=str.split(’’);  
function replacer(char){  
switch(char){  
case ‘\<’:  
return ‘\<’;  
case ‘\>’:  
return ‘\>’;  
case ‘&’:  
return ‘&’;  
case ‘"’:  
return ‘"’;  
case “’”:  
return ‘’’;  
default:  
return char;  
}  
}  
for (var i=0;i\<arr.length;i++){  
arr[i]=replacer(arr[i]);  
}  
str=arr.join(’’);  
// 🙂  
return str;  
}

convertHTML(“Dolce & Gabbana”);

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 2, 2021, 11:59am UTC](https://forum.freecodecamp.org/t/convert-html-entities-problem-in-a-for-loop/158194/10 "2021-02-02T11:59:53Z")

</div>

Closed on February 2, 2021, 11:59am UTC


