Understanding the replace method

Understanding the replace method
0.0 0

#1

So I have an understanding of how the replace() works. However, I don’t know how to fully implement it.

For example, say I wanted to capitalize each letter in a sentence.

I.E. hello everyone, how are you?..would be…Hello Everyone, How Are You?

This example is from https://www.freecodecamp.org/challenges/title-case-a-sentence.

One of the hints states I should use the replace method. But I don’t know how to tell my code to look for only the first letter in a word.

Any ideas?


#2

Split the sentence into individual words.
For each word, replace the first character with its capital equivalent.

myName = "Joe";
myName[0]    // J 

#3

This is my code so far. The problem is I need every first character in a word to be capitalized. And not just one word. What do you think I should do?

function titleCase(str) {

var array = str.toLowerCase().split(’ ');

return array;
}

titleCase(“I’m a little tea pot”);


#4

Your hint gave me an idea. Thanks.


#5

You’ll want to do something for each member of the array. You can do this through the .map() method, which accepts a function as an argument. Here is an example usage of .map()

let newArray = [1, 2, 3, 4, 5].map(function(element) {
     return element * 2
});

console.log(newArray); // [2, 4, 6, 8, 10]

A little too much detail proceeds below, if you get a bit stuck


In this case, you’ll want to take the first character, and make it an uppercase character; and take the rest of the characters (that’s a substring of the full element, by the way), and make them lowercase.


#6

I’m getting so close to the answer. I’ve been working on this for hours now…LOL How do I make spaces within my string. I don’t want it to be array. I just want to add spaces betwen my words.

function titleCase(str) {

var newSentence = " ";
var test = " ";

var array = str.toLowerCase().split(’ ');

for(var i = 0; i < array.length; i++){

   var upperCaseWord = array[i];
   newSentence = upperCaseWord;  
   newSentence = newSentence.charAt(0).toUpperCase()  + newSentence.slice(1);
   test = test + newSentence;

}

return test;
}
titleCase(“I’m a little tea pot”);


#7

This link will help you.


#8

My code is not passing the test, but everything is fine. Any ideas why?

function titleCase(str) {

var newSentence = " ";
var test = “”;

var array = str.toLowerCase().split(’ ');

for(var i = 0; i < array.length; i++){

   var upperCaseWord = array[i];
   newSentence = upperCaseWord;  
   newSentence = newSentence.charAt(0).toUpperCase()  + newSentence.slice(1);
   test += newSentence + " ";

}

return test;
}
titleCase(“sHoRt AnD sToUt”);


#9

You’re adding an unnecessary space at the end of your original sentence.

TIP: I suggest using the .join command.

// given an array "a"
a[0]="aaaaa"
"aaaaa"
a[1]="bbbb"
"bbbb"
a.join(' ')
"aaaaa bbbb"

#10

I would but I keep getting this error message.

TypeError: test.join is not a function. I don’t know how I’m using this function wrong…

function titleCase(str) {

var newSentence = " ";
var test = " ";

var array = str.toLowerCase().split(’ ');

for(var i = 0; i < array.length; i++){

   var upperCaseWord = array[i];
   newSentence = upperCaseWord;  
   newSentence = newSentence.charAt(0).toUpperCase()  + newSentence.slice(1);
   test += newSentence;

}

return test.join(’ ');

}

titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT!”);


#11

The join method is for joining array elements. Since test is a string, it will not work. @owel was suggesting you use an array in your solution to be able to use join.


#12

TIP: You can assign back a new value to your array[i] element… after you capitalize the word. Then you can use .join() in your array to reconstruct the sentence.

Of course, going back to your original solution, you can just remove the trailing blank space before returning the value.


#13

I almost shed a tear,thanks everyone…

function titleCase(str) {

var newSentence = “”;
// var test = [];
var upperCaseWord = [];

var array = str.toLowerCase().split(’ ');

for(var i = 0; i < array.length; i++){

   newSentence  = array[i];  
   newSentence = newSentence.charAt(0).toUpperCase() + newSentence.slice(1);
   upperCaseWord.push(newSentence); 

}

  return upperCaseWord.join(' ');

}

titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT!”);