Write a function to return a camel Case string with 'ROD' in each space

Write a function called toRodCase that takes in a string and returns it as a camel cased string with the ‘ROD’ between each word instead of spaces.

toRodCase(“Hello there stealth warrior”) // should return ‘helloRODThereRODStealthRODWarrior’

toRodCase(“I am excited to learn how to code”) // should return ‘iRODAmRODExcitedRODToRODLearnRODHowRODToRODCode’

Here is my code below , btw this my first post in the any forum.

function toRodCase(str){
 let lowerCased = str.toLowerCase();
 splitstring = lowerCased.split(' ');
 let camelized = '';
 
 for (let i = 0; i < splitstring.length; i++){
   camelized += splitstring[i][0].toUpperCase();
   for(let j = 0; j < splitstring[i].length-1; j++){
     camelized += splitstring [i][j+1];
   }
   if (i<splitstring.length-1){
     camelized += 'ROD';
  }
  //---------need help here------
  //if (i=splitstring.length-0){
    //camelized += splitstring.toLowerCase();
  //}
 }
   return camelized +'';
 }

This is what I am getting ‘HelloRODThereRODStealthRODWarrior’

  • I need to make the “H” a lower case…any thoughts?

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Hi, here’s a solution to the problem at hand. I hope it helps you. Greetings.

const capitalize = word => {
	return word.substring(0, 1).toUpperCase() + word.substring(1);
}

const toRodCase = str => {
	let arrOfWords = str.toLowerCase().split(" ");
    arrOfWords.forEach((value, index, arr) => {
  	if(index != 0)
  		arr[index] = capitalize(value);
  });
  return arrOfWords.join("ROD");
}

let str1 = "Hello there stealth warrior";
let str2 = "I am excited to learn how to code";

let weirdStr1 = "helloRODThereRODStealthRODWarrior";
let weirdStr2 = "iRODAmRODExcitedRODToRODLearnRODHowRODToRODCode";

// Testing
console.log(toRodCase(str1) === weirdStr1); // true
console.log(toRodCase(str2) === weirdStr2); // true

Hi @LMGA!

Welcome to the forum!

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?

const capitalize = word => {
	return word.substring(0, 1).toUpperCase() + word.substring(1);
}

Also this line here is throwing me off, not well versed with the arrow function =>

arrOfWords.forEach((value, index, arr) => {
  	if(index != 0)
  		arr[index] = capitalize(value);
  });

this will make the first letter of any word upper case, that’s why you get the first letter upper case

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?

an if statement could be a way

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:

arrOfWords.forEach(function(value, index, arr) {
  	if(index != 0)
  		arr[index] = capitalize(value);
  });

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 +'';
 }

Saludos

I’ll keep that in mind. Greetings.

Super, I had to read up on the question mark , colon operand for the if statement.
I am still new to all this, thank you for the explanation.

Have a great day!