Spinal tap challenge

Tell us what’s happening:
Describe your issue in detail here.
here is my code:


function spinalCase(str) {
  
 let myStr=str.replace(/\s+/g, '-');
 myStr=myStr.toLowerCase()
 console.log(myStr)
 return myStr;
}

spinalCase('This Is Spinal Tap');

so this code doesnt pass when the input dont have a whitespace. how do i account for adding dashes when there is no whitespace to replace?

   **Your code so far**

function spinalCase(str) {
 
let myStr=str.replace(/\s+/g, '-');
myStr=myStr.toLowerCase()
console.log(myStr)
return myStr;
}

spinalCase('This Is Spinal Tap');
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Spinal Tap Case

Link to the challenge:

Have you encountered capture groups yet? I found them to be really useful in the no-whitespace case. When there is no whitespace, how do you (not your code, but in your head) break up the line?

1 Like

i encountered campture groups from a section on regex. but didnt bother using it. didnt think i had to . I suppose in my head i would have to separate them with spaces. by adding the whitespace back again. i looked up an article and it mention the join function but i get a type error when i try it.

Separate them with spaces…where?

I mention capture groups because they might be useful. For example, if we know a “word” should follow the format “one uppercase followed by zero or more lowercase,” then if we use a capture group to find this pattern, we can use that capture group in the replace statement as "-$1" (the $1 represents our captured string, being inserted).

The sneaky bit is in the teletubbies sentence. :stuck_out_tongue_winking_eye:

ok i used capture groups , i got so close but then when i add the -$1 , it brought the space back . here was the changes i made to that code.

str=str.replace(/(\s+)/g, "-$1");

Sure did bring that space back, because that’s what you’re capturing. Try following the “any number of spaces” regex by the actual capture group you want. You don’t want to capture the spaces, they don’t matter. You want to capture what you want to keep:

str.replace(/\s*( the actual word capture here )/g, "-$1")

And note that i used \s* rather than \s+ - any idea why?

1 Like

yes, that makes sense becaues 0 or more whitespace.

Exactly right. So what might the general-use word capture look like?

i know u want to capture just the words , without the spaces. just stuck on how to put it into code.

Are you using some site that tells you what your regex is actually doing as you build it? I was thinking regexr, but i misremember the url.

no, i wasnt even aware of that site. i would google a certain keyword as im working . many of the sites i use is stack overflow or mozilla.org because those are what pop up frequently

So the pattern to split up ThisIsACapCaseString into unique words. In plain English, how do you know where to break the string?

search through the line and look for the first group of text you see that registers as a word.

i found the site you linked, dont know why it didnt work but when i look it up on google and click on the link it goes to it. https://regexr.com/

im perusing the match function, am i going in the right direction?

“Registers as a word” is vague. Analyze your thinking a bit. What is the cue that ThisIsFun should split into This Is Fun?

I used the replace, but there are many ways to do this, i suppose.

ok thanks ill keep working on it til i get it

A good way to work on things like this might be to pull them into a “sandbox site”, like https://replit.com/ - pull in all the known test cases, and set up an empty function to start. I’ve set one up to start from, including the skeleton of the named function and all the test case calls. It’s not as robust as the tests, in that it isn’t actually running the test suite with a pass/fail, but it will let you develop and test it apart from the FCC lesson console thing.

Also, you’ll see some in-depth comments. I often do this as I’m working through a challenging code bit, as it allows me to brain-dump and get my thoughts out, letting me start to organize them some. Another decent practice to get into.

1 Like