Basic Algorithm Scripting: Title Case a Sentence

Basic Algorithm Scripting: Title Case a Sentence
0
#1

Review my code please :slight_smile:

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"));
#2

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…

#3

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

#4

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

#5

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

i tried adding this line but its not loging anything now

#6

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”));

#7

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.

#8

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

#9

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

#10

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
#11

There are different ways to do this, but:

Here’s your function:

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
#12

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”));

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

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

1 Like
#15
// 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"));