Title case a sentence - help please

Can someone give me a hand with this challenge?
I am not sure on how to remove the first element of each word of the array, and then replace it with my upperCase Variable…Do I use another for loop to do that? What method can I use? How would I then add the rest of the string on ?

  **Your code so far**

function titleCase(str) {
//first letter of each word capitalized, split into an array, for loop to say [i][0] first letter capitalised then add on for each iteration the rest of the string
//use slice (start index, delete count, value)
let lowerCase = str.toLowerCase()
let splitArray = lowerCase.split(' ')
let upperCase = [];
let newArray = [];

for (let i= 0; i < splitArray.length; i++){
  
  upperCase = splitArray[i][0].toUpperCase();
  newArray = splitArray[i][0].replace(upperCase)
   //i want to create a string with first letter removed from each word. Then add on upperCase to each word through each loop...

  console.log(newArray);
}
return newArray;
}

titleCase("I'm a little tea pot");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62

Challenge: Title Case a Sentence

Link to the challenge:

I added some comments

function titleCase(str) {
  let lowerCase = str.toLowerCase();
  let splitArray = lowerCase.split(' ');
  console.log("splitArray:", splitArray); // What is in here?
  let upperCase = [];
  let newArray = [];

  for (let i = 0; i < splitArray.length; i++) {
    console.log("i:", i);
    upperCase = splitArray[i][0].toUpperCase();
    console.log("upperCase:", upperCase);
    newArray = splitArray[i][0].replace(upperCase);
    // What does replace do?
    // What is the syntax for it?
    // Do you want to overwrite newArray each loop iteration?
    console.log("newArray:", newArray);
  }
  return newArray; // This is an array... is the challenge asking you to return an array?
}
1 Like

Can you split the array into two arrays? One array of the first letter and a second with everything but the first letter? In JS, how do you either make a copy of an array without the first element, or remove the first element? Then you can do what you need and join them.

Does that make sense?

2 Likes

The comments helped a lot thank you. I just wanted to know how whether a index of a element in the array can be used as a search value in the .replace method. For example:

newArray = splitArray[i].replace([0], upperCase);

I know this incorrect but I am not sure what else could be done.

Also I want to update the newArray with every iteration rather than overwrite completely, as in each word that it iterates through the first letter is changed.

I agree that I need to return the new str and not an array

my code now looks like this which is good as I know have two arrays but have no idea how to merge them at the specific index, Would I use splice?

function titleCase(str) {
  let lowerCase = str.toLowerCase();
  let splitArray = lowerCase.split(' ');

  console.log("splitArray:", splitArray);

  let capsArray = [];
  let restOfArr = [];
  let newStr = ''
  let newArr = [];
  for (let i = 0; i < splitArray.length; i++) {
    console.log("i:", i);
    capsArray.push(splitArray[i][0].toUpperCase());
    restOfArr.push(splitArray[i].slice(1,splitArray[i].length));

    console.log('rest:', restOfArr );
    console.log('capsArray', capsArray)
   //merge each capsArray word at start of each word in restOfArr
  }

  return newStr;
}

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

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

have no idea how to merge them at the specific index, Would I use splice?

splice is for cutting up arrays, not combining them. There are different ways to combine arrays. In this case we want to add one array to the end of another. There are a couple of ways to do this. One of the most direct methods would be to “concatenate” them. I would suggest searching for “how to concatenate two arrays in javascript”.

Thank you that really helped!!


  let lowerCase = str.toLowerCase();

  let splitArray = lowerCase.split(' ');

  //console.log('splitArray:', splitArray)

  let capsArray = [];

  let lowArr = [];

  let newStr = ''

  let newArr = [];

  

  for (let i = 0; i < splitArray.length; i++) {

    //console.log("i:", i);

    capsArray= (splitArray[i][0].toUpperCase());

    lowArr = (splitArray[i].slice(1,splitArray[i].length));

    newArr.push(capsArray.concat(lowArr))

    newStr = newArr.join(' ')

    //console.log('capsArray:', capsArray)

   // console.log('newStr', newStr)

  

  }

  return newStr;

}

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

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