Stuck on Spinal Tap Case [SOLVED]

Stuck on Spinal Tap Case [SOLVED]
0

#1

Hi I’ve been working on this one for a few days now. Just not sure how I fix this piece of code to complete the challenge. Any advice/pointers?

function spinalCase(str) {
  var regex = /[\_+ |\s+]/g;

  str = str.replace(/[A-Z]/g, function(match, offset) {  
    return (offset ? '-' : '') + match.toLowerCase();
  });
 
  str = str.replace(regex, '');
 
  return str;
}
 
spinalCase("Teletubbies say Eh-oh");

#2

Did a quick try and my final code does this (pseudocode):

Add space before each capital letter.
Change string to lowercase.
Replace non-letters with space.
Trim spaces (strings have such method).
Replace spaces with dashes.

There are ninjas who can write one line regexes, but I’m not one of them :unamused:


#3

Thanking you! I will give that a go.

EDIT: That’s worked nicely, Thanks @jenovs


#5

@Icey You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

We have set your post to unlisted. Thanks for your understanding.


#6

I updated your code. Here is my solution for the problem.
function spinalCase(str) {
var re = /(\ba-z)/g;
str = str.replace(re, function(x){return x.toUpperCase();});
var regex = /[-_+ |\s+]/g;
str = str.replace(regex, “”);
str = str.replace(/[A-Z]/g, function(match, offset) {
return (offset ? ‘-’ : ‘’) + match.toLowerCase();
});

return str;
}