# Basic Algorithm Scripting: Title Case a Sentence

anyway around using trim() function without changing majority of the code?

``````function titleCase(str) {
let str1 = '';
str = str.toLowerCase();
let arr = str.split(' ');
for (let i = 0; i < arr.length; i++) {
str1 += arr[i].replace(/^\S/, (x) => x.toUpperCase()) + ' ';
}
return str1.trim();
}
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));
``````

Push the words to an array and use `join` once all the words have been added, or don’t add a space of you’re on the last item in the array…

im not sure if that will work… that will turn out to be a new method i think

just don’t add a space if the i = arr.length-1

if (i === arr.length -1) {
str1 += arr[i].replace(/^\S/, (x) => x.toUpperCase());
}

Would this be the correct way to go about?

function titleCase(str) {
str1 = ‘’;
str = str.toLowerCase();
let arr = str.split(’ ');
for (let i = 0; i < arr.length-1; i++) {
str1 += arr[i].replace(/^\S/, (x) => x.toUpperCase()) + ’ ';
}
str1 += arr[arr.length - 1].replace(/^\S/, (x) => x.toUpperCase());
return str1;
}
console.log(titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”));

Well, it’ll work. You’re repeating quite complex code, and basing logic on specific array position (which is error-prone). But it’ll work.

how should i be doing it? i couldnt figure it out

how would u have done it to include all scenarios using the second option of not adding a space only for last item

you have a line in your for loop (in the original code) that does the replacing and then it concats a ’ ’ to the string (so the ’ ’ is added each time right?)

Just put the + ’ ’ part as a separate step (inside the for loop) and make it happen for every i except the last one.

1 Like

There are different ways to do this, but:

``````function titleCaseWord(word) {
return word.replace(/^\S/, x => x.toUpperCase());
// Or this:
// return word[0].toUpperCase() + word.slice(1);
}
``````

So

``````function titleCase(str) {
const words = str.split(' ');
const titlecasedWords = [];

// Any loop would do:
for (const word of words) {
let w = titleCaseWord(word);
titlecasedWords.push(w);
}

return titlecasedWords.join(' ');
}
``````

Same as

``````function titleCase(str) {
return str.split(' ').map(titleCaseWord).join(' ');
}
``````

Or do what @hbar1st says

1 Like

do you mean for me to do this?

function titleCase(str) {
str1 = ‘’;
str = str.toLowerCase();
let arr = str.split(’ ');
for (let i = 0; i < arr.length-1; i++) {
arr[i].replace(/^\S/, (x) => x.toUpperCase());
}
str1 = arr.join(" ");
return str1;
}
console.log(titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”));

hmmm doesnt work… either it doesnt capitalize or doesnt save it… what im failing to understand is how i can write the same code for every value of i except the last value i.e. i == arr.length-1

edit: would this do the trick…

function titleCase(str) {
str1 = ‘’;
str = str.toLowerCase();
let arr = str.split(’ ');
for (let i = 0; i < arr.length; i++) {
if (i < arr.length - 1) {
str1 += arr[i].replace(/^\S/, (x) => x.toUpperCase()) + ’ ';
} else {
str1 += arr[i].replace(/^\S/, (x) => x.toUpperCase());
}
}
return str1;
}
console.log(titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”));

``````for (let i = 0; i < arr.length; i++) {
str1 += arr[i].replace(/^\S/, (x) => x.toUpperCase());
if (i < arr.length-1) {
str1 += ' ';
}
}
``````
1 Like

thank you… i was trying too hard in the wrong direction, making it more complex

1 Like
``````// Basic Algorithm Scripting: Title Case a Sentence

function titleCase(str) {

// Regular expression matches first char of all words in the string
// with any non-whitespace character that is the beginning of the string
// OR has a space in front of it
let regex = new RegExp(/(^|\s)\S/, "gi");

// First convert all chars to lower case, then apply upper case to all regex matches
str = str.toLowerCase().replace(regex, (x) => x.toUpperCase());

return str;
}

console.log(titleCase("sHoRt AnD sToUt"));
``````
1 Like