"Concatenating" string methods

Tell us what’s happening:
Hi guys, I solved this problem thanks to a video and I understand the general procedure but I still have some questions:

1- I didn’t know I could “concatenate” string methods (inside the code:
str.toLowerCase().split(" ")). Is this a good practice? Can I do this similar “concatenation” with array methods as well?

2- I don’t understand how exactly the + operator works inside the for loop body. It’s joining the 0-index of each element of words with their respective remaining characters, but how the program knows that it needs to join the characters without spaces between them?


Your code so far

function titleCase(str) {
var words = str.toLowerCase().split(" ") 
for (var i = 0; i < words.length; i++){
   words[i] = words[i][0].toUpperCase() + words[i].slice(1);
return words.join(" ");
titleCase("I'm a little tea pot")// returns: I'm A Little Tea Pot

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0.

Challenge: Title Case a Sentence

Link to the challenge:

  1. str.toLowerCase().split(" ") is an example of “method chaining”. It’s perfectly valid to use, as long as it doesn’t result in code that is difficult to read.

  2. The + operator is the concatenation operator. It combines two strings together (concatenates them). So greeting = "Hello " + username can assign the string “Hello Resurrexit” to the variable greeting.

1 Like

if you don’t explicity concatenate a space, the space is not added

let hello = "Hello";
let world = "World";

// with space:
let string = hello + ' ' + world
console.log(string); // "Hello World"

// without space
let string2 = hello + world;
console.log(string2); // "HelloWorld"
1 Like