Monthly Archives: April 2017

JavaScript30 – Day 17

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
  1. Regex to replace a/an/the with ' '
    function strip(bandName){
    
      return bandName.replace(/^(a |the |an )/i,'').trim();
    
    }
    
  2. Ternary operator - ?: is called ternary operator
    return strip(a) > strip(b) ? 1 : -1
    
  3. 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);
    
  4. Output with/without commas
    sortedBands.map(band => `
  5. ${band}
  6. `); //will give you commas after the names: ["
  7. Anywhere But Here
  8. ", "
  9. The Bled
  10. ", "
  11. Counterparts
  12. ", "
  13. The Devil Wears Prada
  14. ", "
  15. The Midway State
  16. ", "
  17. Norma Jean
  18. ", "
  19. Oh, Sleeper
  20. ", "
  21. An Old Dog
  22. ", "
  23. Pierce the Veil
  24. ", "
  25. The Plot in You
  26. ", "
  27. Say Anything
  28. ", "
  29. A Skylit Drive
  30. ", "
  31. We Came as Romans
  32. "]
    sortedBands.map(band => `
  33. ${band}
  34. `).join(''); //will give
  35. Anywhere But Here
  36. The Bled
  37. Counterparts
  38. The Devil Wears Prada
  39. The Midway State
  40. Norma Jean
  41. Oh, Sleeper
  42. An Old Dog
  43. Pierce the Veil
  44. The Plot in You
  45. Say Anything
  46. A Skylit Drive
  47. We Came as Romans
Day 17 code on github.