Skip to content
Advertisement

How do I convert a string to spinal case in JavasScript?

I am stuck on this coding challenge Spinal Tap Case from freeCodeCamp. Essentially I don’t know how to get the last check to execute.

This is the last check: spinalCase("AllThe-small Things") should return "all-the-small-things"

And this is my code:

JavaScript

I do realize the last else ifcondition should go first however I didn’t get the syntax right.

Thanks in advance!

Advertisement

Answer

Here is how I recommend doing it:

JavaScript

JsBin Example

as far as your code, you check for:

if test1 else if test2, then else if test1 and test2, the logic is not correct:

you could try to adding a !test2 or !test1 to the first if checks to get it working:

JavaScript

EDIT:

here is how you can get your code to fire in that last else if, I put a console.log in there to show you here:

JSBin Example

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement