Question: I want to replace second space occurrence of the sentence with a br.

I have tried this but it is deleting the rest.

```
var title = "My Title Needs Brace".split(" ").slice(0, 2).join(" ");

```

Answer: 
That will do the trick:
-----------------------

```
"My Title Needs Brace"
   .split(' ')
   .reduce(function (str, part, i) {
      return str + (i === 2 ? '<br/>' : ' ') + part
   });

// "My Title<br/>Needs Brace"

```

Let's break it and see how it works:
------------------------------------

First, we take the string and **split** it. we'll use `" "` as our separator

```
"My Title Needs Brace".split(' ')

// ["My", "Title", "Needs", "Brace"]

```

Second, we'll use **reduce** to combine the array back into one string

```
["My", "Title", "Needs", "Brace"]
   .reduce(function (str, part) { return str + ' ' + part }, '');

// "My Title Needs Brace"

```

Why **reduce** and not **join**?
The advantage of reduce over join is that it allows us to use a function, which will give us a fine-grained control over how we join back each part of the string

Now, all that left is to replace the 2nd space with `<br/>`,
for that, we'll use the 3rd argument of the reduce function, which stands for the index, and ask:

is this the 3rd part? use `<br/>`
otherwise, use `" "`

```
"My Title Needs Brace"
   .split(' ')
   .reduce(function (str, part, i) {
      return str + (i === 2 ? '<br/>' : ' ') + part
   });

// "My Title<br/>Needs Brace"     

```

Note that this is the index of the string "part", not the spaces between them so the index is 2, not 1.

More about:
-----------

* [split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
* [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)
* [join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)
***maybe that will help :***

```
var title = "My Title Needs Brace".split(" ");
t1=title [0]; My
t2=title[1]; // Title 
t3=title[2]; // Needs 
t4=title[3]; // Brace

```

**you can drew here anything :**

```
var htmlString = '' + t1 +''+ t2 + '<br />' +  t3 +''+ t4 + '';
$('Anywhere').append(htmlString);

```