Javascript Freecodecamp Algorithm #5: Spinal Tap Case

preview_player
Показать описание
Learn how to solve freecodecamp javascript algorithms in various ways! This series is up-to-date with all ES6 and beyond javascript notations
Рекомендации по теме
Комментарии
Автор

Hi, I understood the regex method better but I want to understand the imperative method as well. How does the 'word' string know to get populated with 'c' initially before it starts to fulfil the conditions in the if statement. Because the if statement starts by specifying when it gets to an underscore or space not when it gets to the first character in the string. How does 'word' know what to do with 'c' prior to this pls?

wHyNoTCaTs-efyh
Автор

I used reg exp for the answer and its pretty straight forward and it works:
function spinalCase(str) {

x = new RegExp(/([a-z])([A-Z])/g);
y = new RegExp(/\s+|_/g);

return str.replace(x, "$1-$2").replace(y, "-").toLowerCase();
}

zarakhaliq
welcome to shbcf.ru