Convert HTML Entities / string.replace()

Tell us what’s happening:

i have no idea ow to use str.replace();
not sure i understand the documentation on how to use a function as the thing to be replaced with.
and i’m not sure how to make a “dictionary” of keys to reference what to be replaced with. (switch statement? object? array with .map() ?)

in short , absolutely lost

ignore my code, its my 127th try, and absolutely useless.

mostly what i want to know is how to use string.replace(); since thats what this exercise seems to be about.

Your code so far

function convertHTML(str) {
  // :)
  var Regcheck = /\W\s/gi;
   var htmlListObj = {'&':"&",
                      "<":"&lt;",
                      ">":"&gt;",
                      "'":"&quot;",
                      '"':"&apos;"};

 /* var a = str.split(" ");
 for(var i = 0;i<a.length;i++){
   if(a[i] === Regcheck )
 }*/
  var b = "";
 for(var i = 0;i<str.length;i++){
   if(str[i].match(Regcheck) === htmlListObj[i]){
     return str.replace(htmlListObj[key],htmlListObj[value]);
   }
 }
  //return b;
  
}

convertHTML("Dolce & ^ $ Gabbana");

Your browser information:

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

Link to the challenge:

is concatenating the result int o the output of the loop a “best practice” or the only way?

i’m going to come back here once i solve this, i need to know what the shortest way of solving this is.

i figured it out


function convertHTML(str) {
  // &colon;&rpar;
  var Regcheck = /\W\s/gi;
   var htmlListObj = {'&':"&amp;",
                      "<":"&lt;",
                      ">":"&gt;",
                      '"':"&quot;",
                      "'":"&apos;"};

 var a = str.split("");
 var b =[];
  
   
    for (var i in a) {
    
      if (htmlListObj.hasOwnProperty(a[i])) {
          b.push( htmlListObj[a[i]]);
       }else{b.push(a[i]);} 
     }

var c = b.join("");
 
  return c;
  
}

convertHTML('Stuff in "quotation marks"');

whats the shorter way of doing this? how would i use .replace(); ?

you can shove multiple methods on the end of a return statement!!! eureka! i would have never figured that out

shouldn’t there be a way to make a single function to cover all possibilities, and make one replace statement?

what does => do?
i don’t know where to find that in the documentation.
and when did you declare what find is?

that was EXACTLY what i was looking for!
how did you know to use the function in .replace() like that?

i did, but i didn’t understand HOW to implement it like you did, HOW did you do it?