Convert HTML Entities - Please help! (Infiinite loop?)

Convert HTML Entities - Please help! (Infiinite loop?)
0

#1

Tell us what’s happening:
Hey, could I have some help? What’s wrong in my code?

It says in repl.it when I run it that it is:

RangeError: Potential infinite loop. You can disable this from settings.
    at convertHTML:15:107
    at eval:26:1

And when I run it in Chrome console it stops working (the actual FCC website), so I suspect an infinite loop, but I don’t see where it is??

Your code so far


function convertHTML(str) {
let entities = { //Object created for later use
'&':'&',
'<':'&lt;',
'>':'&gt;',
'"':'&quot;',
'\'':'&apos;'
}

str=str.split(""); //str coming into meta-functionis converted to array
let keyz=Object.keys(entities); //Assigning every key in the object "entities" to the array "keyz"
let valuez=Object.values(entities); //Assigning every value in the object "entities" to the array "valuez"

for (let i=0; i<str.length; i++) { //Looping over str's elements
for(let j=0;j<keyz.length;i++){ //Looping over every key in entities
if (keyz[j]===str[i]) { //If current key of entities that is being looped over, equals the element of string which is currently being looped over
str[i]=valuez[j]; //Then make that element of string equal to property of key currently looped over
} //I thought this should work because keys and properties will be at same index of their separate arrays
}
}
console.log(str); //Test
str.join("");
return str;
}

convertHTML("Dolce & Gabbana");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/convert-html-entities/


#2

The line above is one of the problems in your solution. Do you really want to increment i here?

The next problem is in the following lines:

  str.join("");
  return str;

The join method returns a new string and does not change the value of the string on which it is called. You can either assign the result of the join to a variable and return that variable or just return the result of the join (use return keyword on same line).


#3

Thank you! That solved it for me and I understand what was wrong