Adding stuff all at once

Hey guys, I have a question.
I have found a solution and I would like to work it through.
How can I add my variable replacer all at once instead of once every iteration?
Thanks.

  **Your code so far**

function spinalCase(str) {
const regex = /\s/g
console.log(str);
for (let i = 0; i < str.length; i++) {
  const addMe = '-'
  if (str[i].toUpperCase() === str[i]) {
    const replacer = addMe + str[i];
    console.log(str.replace(str[i], replacer).toLowerCase());
  }

}
}

spinalCase("thisIsSpinalTap");

/* loop over str
if there an upperCase letter
replace letter with a '-' and a smaller case letter or add '-' before the letter and then lowerCase string.
if there isn't replace spaces with '-'.
*/
  **Your browser information:**

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

Challenge: Spinal Tap Case

Link to the challenge:

I’d focus on getting this code to pass first - it isn’t quite there. Once you do that, then you can clean up the code.

I am sorry, I am a bit confused by your answer. I think I need to figure it out and it should pass.

After I add another if.

I thought this meant that you thought that you had a solution, lol

You have two big problems.

  1. your function doesn’t return anything

  2. you are logging and then throwing away the result of the replace

Because it is not quite there yet so I don’t want to return, I console.log.

I have the issue that I asked the question about, I believe if I solve it I should continue and I will pass the challenge, but I am not sure how to figure out the question I asked, so I have come here.

And my answer to that is that I would stick with a loop and get an approach with a loop working first.

Your output in the console looks like this

thisIsSpinalTap
this-isspinaltap
thisis-spinaltap
thisisspinal-tap

because of this

From String.prototype.replace() - JavaScript | MDN

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement .

The original string is left unchanged.

I made some changes but I am still stuck. I’ll try to think it over, Thanks.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.