Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/056242238d521c6d631d to your computer and use it in GitHub Desktop.
Save anonymous/056242238d521c6d631d to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Convert HTML Entities
// Bonfire: Convert HTML Entities
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-convert-html-entities
// Learn to Code at Free Code Camp (www.freecodecamp.com)
/* Convert the characters "&", "<", ">", '"' (double quote), and "'" (apostrophe), in a string to their corresponding HTML entities. */
function convert(str) {
// &colon;&rpar;
// Split by character to avoid problems.
var temp = str.split('');
// Since we are only checking for a few HTML elements I used a switch
for (var i = 0; i < temp.length; i++) {
switch (temp[i]) {
case '<':
temp[i] = '&lt;';
break;
case '&':
temp[i] = '&amp;';
break;
case '>':
temp[i] = '&gt;';
break;
case '"':
temp[i] = '&quot;';
break;
case "'":
temp[i] = '&apos;';
break;
}
}
temp = temp.join('');
return temp;
}
convert("Dolce & Gabbana");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment