JavaScript30 is 30-day vanilla javaScript coding challenge by
Wes Bos (
@wesbos).
The purpose of these posts is to reflect my troubles and moments of ahhaa. Sorting band names without a/an/the
Lessons from Day 17 — a/an/the:
Demo - Day 17
- Regex to replace a/an/the with ' '
function strip(bandName){
return bandName.replace(/^(a |the |an )/i,'').trim();
}
- Ternary operator - ?: is called ternary operator
return strip(a) > strip(b) ? 1 : -1
- How to write a function in three ways:
//first
const sortedBands = bands.sort(function(a,b){
return strip(a) > strip(b) ? 1 : -1
});
//as arrow-function
const sortedBands = bands.sort ((a,b) =>{
return strip(a) > strip(b) ? 1 : -1
});
// if the only thing you are doing in function is returning - you can do implicit return
const sortedBands = bands.sort((a,b) => strip(a) > strip(b) ? 1 : -1);
- Output with/without commas
sortedBands.map(band => `
- ${band}
`);
//will give you commas after the names:
["- Anywhere But Here
", "- The Bled
", "- Counterparts
", "- The Devil Wears Prada
", "- The Midway State
", "- Norma Jean
", "- Oh, Sleeper
", "- An Old Dog
", "- Pierce the Veil
", "- The Plot in You
", "- Say Anything
", "- A Skylit Drive
", "- We Came as Romans
"]
sortedBands.map(band => `
- ${band}
`).join('');
//will give
- Anywhere But Here
- The Bled
- Counterparts
- The Devil Wears Prada
- The Midway State
- Norma Jean
- Oh, Sleeper
- An Old Dog
- Pierce the Veil
- The Plot in You
- Say Anything
- A Skylit Drive
- We Came as Romans
Day 17 code on github.