Basic algorithm scripting: Title_Case_a_Sentence(1)

hello, I’m having trouble coming up with a way of solving this problem.

title case a sentence - Basic algorithm scripting

I’m trying to figure out after spliting the string into an array then lowercase the string, after that how to use { toUpperCase} with replace() method.
then formulating a for loop to iterate through the array and finally joining them into one string.

I’m kinda-ish stuck there.

Hey there,

it makes sense to show us some code.

Your algorithm seems correct.

You can add a callback function as the second argument to the replace-method. You can read about it on MDN String.prototype.replace. Their example is fairly complex, but in the end you can pass the first character and return it in uppercase.

here it is:


function titleCase(str) {
let lowerCase = str.toLowerCase().split(" ");
let upperCase = lowerCase.replace(/(0)/i, lowerCase.toUpperCase())
for( let i = 0; i > lowerCase.length; i++){
; 
}
}

titleCase("I'm a little tea pot");

I’m abit lost

Log the variables after each step and see what this actually is.

When you split a string, you get an array.

What do you want to do with this next line:

I wanted to use Regexp to capitalize the first letter of the string, either use regexp or charAt(), and then from there join the array using a for loop.

I am abit lost there on how to use either of those methods. and how the for loop comes in.

But you’re trying to replace something in lowerCase, that’s an array.
You first have to go into each item of lowerCase.

// iterate through lowerCase (e.g. for loop)
  // make first character uppercase (e.g. replace method)
  // return item
// join all strings of array back together

Thanks, I scratched my head abit but I managed to solve it

1 Like

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