Algorithm Spinal Tap Case

🚩 Remember to use Read-Search-Ask if you get stuck. Try to pair program 👥 and write your own code 📝

🏁 Problem Explanation:

Convert the given string to a lowercase sentence with words joined by dashes.

Relevant Links

💬 Hint: 1

Create a regular expression for all white spaces and underscores.

try to solve the problem now

💬 Hint: 2

You will also have to make everything lowercase.

try to solve the problem now

💬 Hint: 3

The tricky part is getting the regular expression part to work, once you do that then just turn the uppercase to lowercase and replace spaces with underscores using replace().

try to solve the problem now

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

🔰 Basic Code Solution:

function spinalCase(str) {
  // Create a variable for the white space and underscores.
  var regex = /\s+|_+/g;

  // Replace low-upper case to low-space-uppercase
  str = str.replace(/([a-z])([A-Z])/g, '$1 $2');

  // Replace space and underscore with -
  return str.replace(regex, '-').toLowerCase();
}

// test here
spinalCase('This Is Spinal Tap');

🚀 Run Code

Code Explanation:

  • regex contains the regular expression /\s+|_+/g, which will select all white spaces and underscores.
  • The first replace() puts a space before any encountered uppercase characters in the string str so that the spaces can be replaced by dashes later on.
  • While returning the string, another replace() replaces spaces and underscores with dashes using regex.

🌻 Intermediate Code Solution:

function spinalCase(str) {
  // Replace low-upper case to low-space-uppercase
  str = str.replace(/([a-z])([A-Z])/g, '$1 $2');
  // Split on whitespace and underscores and join with dash
  return str.toLowerCase().split(/(?:_| )+/) .join('-');
}

// test here
spinalCase('This Is Spinal Tap');

🚀 Run Code

Code Explanation:

  • Similar to the first solution, the first replace() puts a space before any encountered uppercase characters in the string str so that the spaces can be replaced by dashes later on.
  • Instead of using replace() here to replace whitespace and underscores with dashes, the string is split() on the regular expression /(?:_| )+/ and join()-ed on -.

Relevant Links

🏆 Credits:

If you found this page useful, you may say thanks to the contributors by copying and pasting the following line in the main chat:

Thanks @Rafase282 @anuragaryan for your help with Algorithm: Spinal Tap Case

📋 NOTES FOR CONTRIBUTIONS:

  • ⚠ DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. 🚥
  • Please add your username only if you have added any relevant main contents. (⚠ DO NOT remove any existing usernames)

See 👉 Wiki Challenge Solution Template for reference.