I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.
If you post a full passing solution to a challenge, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.
Please help me understand why the syntax your using is const instead of function? and why is the capitalize function outside of the toRodCase function?
Ok , I understand that.
Just to review –
The first for loop iterates through the array of lower cased letters to upper case the first letter of each index
The second nested for loop iterated through the lower case array and returns all the letters except the upper cased letter in the parent for loop.
The first if statement takes care swapping the spaces out for the word ROD
Where and how do I address the first upper case letter to remain a lower case?
Would I need a second if statement?
Hi. In JavaScript, functions are a data type so you can assign them to a variable, but I chose a constant because it will never vary. The function you assign to it is called an anonymous function. Also, functions can be declared inside another function, but I chose to declare it outside.
Writing in the ES5 syntax could be written as follows:
var capitalize = function(word) {...}
Regarding the other line of code you mention, I also used an anonymous function. Most of the array methods require one. I could have written it like this:
Looking at your code you almost have the answer, you just need to add a conditional if, so that it capitalizes all words except the first one, this is true for i != 0.
function toRodCase(str){
let lowerCased = str.toLowerCase();
splitstring = lowerCased.split(' ');
let camelized = '';
for (let i = 0; i < splitstring.length; i++){
camelized += i != 0 ? splitstring[i][0].toUpperCase() : splitstring[i][0];
for(let j = 0; j < splitstring[i].length-1; j++){
camelized += splitstring [i][j+1];
}
if (i<splitstring.length-1){
camelized += 'ROD';
}
}
return camelized +'';
}