Introduction
The Spinal Tap Case challenge requires converting a string into a specific format. You are provided a function with one argument, str, that contains a sentence. Your task is to transform this string so that every character is lowercase and all words are joined by dashes (-) instead of spaces, for instance, our str is This Is Spinal Tap so solving this we have to convert all the letters in lowercase this is spinal tap then we have to use - dashes instead of spaces so our final output will be this-is-spinal-tap. This is the one test case, but we have more than one test case in the challenge, and we must write a function that could solve all of them. I used this test case for your understanding, like how our final output will look. However, we will explore all the test cases before solving the challenge.
Examples:
Test Case 1:
- Str = This Is Spinal Tap
- Output = this-is-spinal-tap
In this test case, this is a simple test case where we have to replace all the spaces with - dashes, and convert all the characters into lowercase.
Test Case 2:
- Str = thisIsSpinalTap
- Output = this-is-spinal-tap
In this test case, we have to separate each word and then add - dashes for joining the words finally converting all the characters into lowercase, as you can see we can solve this test case by identifying the logic: before each capital letter we have to add - dashes, then the str will be like this this-Is-Spinal-Tap now we need to convert all the characters into lowercase, then we will have our final output this-is-spinal-tap.
Test Case 3:
- Str = The_Andy_Griffith_Show
- Output = the-andy-griffith-show
In this test case, we have to do the same as we did with the previous test cases, but the unique thing here is that we have to cope with the underscores _ which means if there is any underscore within our string we need to replace that with dash - as well and bring our string into spinal case state the-andy-griffith-show.
These are a few test cases, we have to write our function based on them so we can solve this challenge effectively.
Theoretically solve
Step 1: We need to make sure each word should be lowercase.
Step 2: We need to use dashes for joining each word - instead of spaces.
Step 3: Replace all spaces and underscores (_) with dashes (-) to join the words.
Practically Sove:
function spinalCase(str) {
return str.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+|_/g, '-').toLowerCase();
}
console.log(spinalCase('This Is Spinal Tap'));
// Output: this-is-spinal-tap
Let’s break down this solution for better understanding.
We have a function spinalCase with one parameter str, in this our sentence value is stored. It is quite a simple function, we don’t need to dive deeper.
return str.replace(/([a-z])([A-Z])/g, '$1-$2');
We used built-in functions to solve this challenge, within the function return keyword is used for returning the final output of the function after that str is the string that is given to us to solve the challenge, and replace() is a JavaScript built-in function, used for replacing values one over others.
In JavaScript, for most string built-in functions we use . for working with.
In simple, we are returning our str as a final output, but since we used the replace method our str will be changed due to the replace() method because we used the replace method like this .replace(/([a-z])([A-Z])/g, '$1-$2') which means it will add dashes - between lowercase and uppercase letters (if there is a word like codE so the output will be cod-E).
replace(/\s+|_/g, '-')
Then we used the replace() method once again for replacing underscores _ with dashes -. Finally, we used toLowerCase(), another built-in method in JavaScript used for transforming whole string characters in lowercase.
.toLowerCase()
We have succeeded in writing a function that can solve all the test cases, so our challenge is solved now.