Log
Convert HTML Entities (FCC 273)
10:30
Initial test fails.
Error was treating str.replace()
like an action. Needed
str = str.replace( x, y );
return str;
11:30
After watching this video I’m going to try writing a function into the replace command.
11:38
Done! defined the conditionals with switch
, which feels like the right choice for readability if not brevity.
You don’t have to explicitly pass the regex match to the function call, or use parentheses (which seems syntactically “off”, but hey-ho:
function convertHTML(str) {
var match = /[&<>]/g;
function condRep(match) {
switch (match) {
case "&" :
return "&";
case "<" :
return "<";
case ">" :
return "&rt;";
}
}
str = str.replace(match, condRep);
return str;
}
convertHTML("Dolce & Gabbana < >");
Just realised in the course of adding that… I’ve not included "
or '
. D’Oh.
Fortunately the way I’ve written means it should be an easy fix.
11:58
Updated regex variable with '
and "
and added 2 new cases. Time to test.
11:59
Failed, but I think because I made an incorrect assumtion that >
was &rt;
when in fact it should be >
12:01
Done. Took much longer than it ought because of WhatsApp conversations with fellow coders about unrelated stuff.
Spinal Tap Case
More regex fun.
12:33
Overlooked conditions of the challenge again! Can’t just sub spaces for -
since some of the test cases don’t delimit by space.
Need to
- match words by any non-alphanumeric delimiter or caps
- remove spaces
- insert dashes
- decapitalise
12:44
Backreferences weren’t working because I forgot to enclose them with parentheses within the regex / /
s.
12:57
As my list of search-replace commands grows, it seems a function would be preferable.
14:23
Hmmm. Didn’t know how to deal with conditionals within the function when they’d been matched by subsets of the regex.
eg /([A-Z])|\s|\W/g/
will match any uppercase letter, whitespace characters and non-alphanumeric characters1, and within the replace statement I could use a backreference, but when the pattern is handed into a function how do I test for caps, symbol, space?
After trying a few things that didn’t work, the only likely method seemed to have a new array of letters via [ABCDEFGHIJKLMNOPQRSTUVWXYZ].split('')
or somesuch and that was intolerably clunky to me.
So I’m not sure if my actual solution is any less clunky, as it relies on 4 successive str.replace() statements, one of which contains an anonymous function.
I think it was a pretty poor challenge in terms of being arbitrary test cases. It would have been more difficult but more consistent to have specified how any string may have needed conversion, eg “This%is£A_string+”
Well anyway. It’s done.
Fibonacci sequence
16:44
Finshed this. It was trickier than I thought!
I’ve never had much luck with while
loops and this gave me trouble as well.
The main challenge here was assembling the steps in the right order. I think I made pretty good use of the array.slice(-2)
(which takes the last two items in the array), array.reduce(function(a,b){return a+b}
(to add them) and the ternary conditional to increment the total if odd and leave as is if even.
The stopping condition became the main problem.
I spent ages figuring out where to put another condition that checked the last item in the list (not the total and not nextFib
variable!).
Done.
1 - I found this wasn’t true; -
was matched by \W
but _
wasn’t.