Created
December 3, 2015 02:41
-
-
Save anonymous/056242238d521c6d631d to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Convert HTML Entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { | |
// :) | |
// 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] = '<'; | |
break; | |
case '&': | |
temp[i] = '&'; | |
break; | |
case '>': | |
temp[i] = '>'; | |
break; | |
case '"': | |
temp[i] = '"'; | |
break; | |
case "'": | |
temp[i] = '''; | |
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