Convert HTML Entities: FreeCodeCamp.com Intermediate Algorithm Scripting

preview_player
Показать описание

Fan funding goes towards buying the equipment necessary to deliver 4k videos, 4k webcam, and a high quality microphone better audio. Any support is very appreciated!

My channel is here for aspiring programmers to learn easier and help debug any issues from the many great free resources available on the web.

Check out my other videos going over HTML, CSS, Ruby, Ruby on Rails, Java, JavaScript, Python, PHP, SQL, Command Line, BootStrap, jQuery, and WordPress from CodeCademy, CodeCombat, FreeCodecamp and more!

-~-~~-~~~-~~-~-
Please watch: "How I Became a Developer | My Developer Journey of the Last 3 Years | Ask a Dev"
-~-~~-~~~-~~-~-
Рекомендации по теме
Комментарии
Автор

I was stuck on this one for two days, similar to old challenges, but I got it done :)

wombat
Автор

the easy way using regular expression

function convert(str) {
// :)

return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/''/g, "marks&;").replace(/'/g, "&apos;");
}
convert("Shindler's List");
//

leonel
Автор

Yeah, I noticed you did comments underneath.. but you actually inspired me to move them from above to trailing off to the right..

JonathanPohlner
Автор

I feel like you really should have shown this with regular expressions and the replace method.

However I did not think of doing it this way. Good job.

devincohen
Автор

I tired to do it the way they wanted it, but it says they are all wrong but in fact I checked it 3 times with all the arguments, and its comes out with the same answer on my end. Anyways, I thought I would share mine I thought mine looks a little different then everyone else's.




function convertHTML(str) {
var array1 = ['&', '<', '>', '\"', '\''];
var array2 = ['&amp', '&lt', '&gt', '&quot;', '&​apos;'];
var transString;
var boolean = false;

for (var i = 0; i < str.length; i++) {
if (str.indexOf(array1[i]) != -1) {
transString = array1[i];
transString = new RegExp(transString, 'g');
transString = str.replace(transString, array2[i]);
boolean = true;
return transString;
}
}
if (boolean) {
return boolean;
}
}

Cognitoman
Автор

Hey I still don't get it, why you use placeHolder if it's difficult to type?
You could simply use arr

abhishekchoudharykhatkar
Автор

But your function iterates over string even though it may not need to. It also iterates over every character even though it may not need to.


function convert(str) {
var regex = /[&<>"']/g;

var HTML = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&apos;'
};

if (regex.test(str)){
var characters = str.match(regex);
characters.forEach(x => str = str.replace(x, HTML[x]));
}

return str;
}

aliquewilliams
Автор

Dude it's so much more trouble with in javascript. In php is just htmlentities(); done. Then again there is probably form sanitize library out there somewhere.

JasonCtutorials