Tag: string methods

FCC Bonfire Series 146: No repeats please

No repeats please. This is a tough one, really. We are going to bruteforce this challenge instead of using clever mathematics this time around. Simply because it will let us do a few more things. […]

FCC Bonfire Series 129: Spinal Tab Case

Hello campers of young and old, today, we are going to be converting strings to spinal-tab-case. If you did not know about spinal-tab-case, well, you do now. You just-saw-it-twic… thrice!

Spinal case simply removes all spaces and sets dashes instead, while having the string be all lower-case characters. It sounds easy to achieve, we are replace wizards after all; but we’ll see that some cases might not be so straightforward.

First thing that comes to mind is probably:

function spinalCase(str) {
  return str.replace(/ /g, '-').toLowerCase();

We first replace all spaces with dashes, and then get rid of capital letters. And you see, that works for strings such as:

spinalCase('Hello world!'); //-> hello-world!

spinalCase('I want to see the world BURN.'); //-> i-want-to-see-the-world-burn.

But how about these other case?

spinalCase('Hey_I_got_you_this_time'); //-> hey_I_got_you_this_time

Let’s get rid of those underscores too!

FCC Bonfire Series 128: Convert HTML Entities

This next bonfire will prove useful in the future (you’ll see why when the time comes). We are going to be converting special characters to their HTML entity counterparts.

What is a HTML entity you may ask, you see, some characters have special meanings in HTML, the greater than and lesser than symbols (>, <) for example, are used in HTML tags, and the browser can confuse them if used in plain text. For this reason, these kind of characters should be written as their HTML entity (or entities, each character may be written in a few ways). For example:

  • & -> &amp;
  •  -> &quot;

You can check out the whole list here.

The bonfire will have us replace the following characters (you can take a look at their HTML entity codes in the like above):

  • &
  • <
  • >

We are JavaScript wizards. We got this under control. We know the HTML entity codes, we know the tools JavaScript has to offer. Come on, let’s get to it!