Thursday, July 23, 2020

Spinal Tap Case

From: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/spinal-tap-case

Intermediate Algorithm Scripting: Spinal Tap Case

Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.

spinalCase("This Is Spinal Tap") should return "this-is-spinal-tap".

spinalCase("thisIsSpinalTap") should return "this-is-spinal-tap".

spinalCase("The_Andy_Griffith_Show") should return "the-andy-griffith-show".

spinalCase("Teletubbies say Eh-oh") should return "teletubbies-say-eh-oh".

spinalCase("AllThe-small Things") should return "all-the-small-things".

const spinalCase = str => 
   str.match(/([A-Z]|[a-z])[a-z]*/g).map(word => word.toLowerCase()).join('-');

[
  'This Is Spinal Tap',
  'thisIsSpinalTap',
  'The_Andy_Griffith_Show',
  'Teletubbies say Eh-oh',
  'AllThe-small Things'
].forEach(param => console.log(spinalCase(param)));


Output:
this-is-spinal-tap
this-is-spinal-tap
the-andy-griffith-show
teletubbies-say-eh-oh
all-the-small-things

No comments:

Post a Comment