# JavaScript Strings

**URL:** <https://discourse.mozilla.org/t/javascript-strings/86498>\
**Category:** Learn\
**Created:** [September 28, 2021, 9:02pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498 "2021-09-28T21:02:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Takis](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/takis/32/51892_2.png) [@Takis](https://discourse.mozilla.org/u/Takis)\
**Post date:** [September 28, 2021, 9:02pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498/1 "2021-09-28T21:02:35Z")

</div>

I have done this exercise but something tells me that I did not do it well, can you take a look at it and tell me if it is okay or what was your solution? I was a bit confused because I speak Spanish and the translation was a bit confusing to carry out the exercise.

## [Strings 2](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Strings#strings_2)

In this task you are provided with two variables, `quote` and `substring` , which contain two strings. We would like you to:

1. Retrieve the length of the quote, and store it in a variable called `quoteLength` .
2. Find the index position where `substring` appears in `quote` , and store that value in a variable called `index` .
3. Use a combination of the variables you have and available string properties/methods to trim down the original quote to “I do not like green eggs and ham.”, and store it in a variable called `revisedQuote` .

> let quote = ‘I do not like green eggs and ham. I do not like them, Sam-I-Am.’;  
> let substring = ‘green eggs and ham’;
> 
> // Add your code here  
> let quoteLength= quote.length;  
> let index= 33;  
> let revisedQuote=`I do not like ${substring}`  
> // Don’t edit the code below here!
> 
> section.innerHTML = ’ ';  
> let para1 = document.createElement(‘p’);  
> para1.textContent = `The quote is ${ quoteLength } characters long.`;  
> let para2 = document.createElement(‘p’);  
> para2.textContent = revisedQuote;
> 
> section.appendChild(para1);  
> section.appendChild(para2);

---

<div class="post-metadata">

**Author:** ![mikoMK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mikomk/32/50456_2.png) [@mikoMK](https://discourse.mozilla.org/u/mikoMK)\
**Post date:** [September 28, 2021, 9:48pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498/2 "2021-09-28T21:48:01Z")

</div>

Hi @Takis and welcome to the community 👋

1. Correct ✅
2. You should use the `quote.indexOf()` function instead of counting.
3. Use `quote.slice(0, endIndex)` where `endIndex` has to be calculated by you with `index` and `substring.length`

> **[String.prototype.indexOf() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf)**
>
> The indexOf() method of String values searches this string and returns the index of the first occurrence of the specified substring. It takes an optional starting position and returns the first occurrence of the specified substring at an index...

> **[String.prototype.slice() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice)**
>
> The slice() method of String values extracts a section of this string and
> returns it as a new string, without modifying the original string.

I hope those hints help you. Feel free to ask if you need more help.

Happy Coding!  
Michael

PS: When pasting code here you should use ``` on its own line at the start at end of your code:

```  
Your code  
More code  
Even more…  
```

Even better: Use an online editor like [codepen.io](http://codepen.io)

---

<div class="post-metadata">

**Author:** ![Takis](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/takis/32/51892_2.png) [@Takis](https://discourse.mozilla.org/u/Takis)\
**Post date:** [September 29, 2021, 8:06pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498/3 "2021-09-29T20:06:54Z")

</div>

Thank you very much for the help, now everything is clearer;). and I played around with the code for a while. I show you how I look in the code open;). I added a variable to avoid having to count the characters that, although I knew that the value was 33, I preferred to find a way not to have to put the value manually.  
Point 3 I did not understand how to calculate it with those two variables I did not find the calculation hahaha so I added that variable

> Use quote.slice (0, endIndex) where endIndex should be calculated by you with index and substring.length

How do I calculate it with those variables?  
what would be the calculation?

See the Pen [](https://codepen.io/ltakisl/pen/GREzLJq) by lTakisl ([@ltakisl](https://codepen.io/ltakisl)) on [CodePen](https://codepen.io).

---

<div class="post-metadata">

**Author:** ![mikoMK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mikomk/32/50456_2.png) [@mikoMK](https://discourse.mozilla.org/u/mikoMK)\
**Post date:** [September 29, 2021, 8:42pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498/4 "2021-09-29T20:42:26Z")

</div>

Thank you for sharing the CodePen!

Yes, you used `indexof` correctly. Great! Your feeling about calculating is better than counting is absolutely right. Imaging you are counting in a real project and some weeks or months later you change the string a little bit and forget to update the count. Suddenly your project doesn’t work correctly anymore and you are scratching your head why.

The last one is indeed very tricky, but you get points for creativity 😁  
You can calculate `endIndex` like this:

```auto
let endIndex = index + substring.length + 1;

```

Let’s see why this works:

- `index` is the start of our substring that we calculated before
- Then we add the length of the substring. We are now at the period after “ham”
- We add one because the end index in `slice` means the first character that is not included. So this means the space after “ham.” (and anything after that) is not included.

Cheers!  
Michael

---

<div class="post-metadata">

**Author:** ![Takis](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/takis/32/51892_2.png) [@Takis](https://discourse.mozilla.org/u/Takis)\
**Post date:** [September 29, 2021, 11:45pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498/5 "2021-09-29T23:45:24Z")

</div>

The truth is that I am very happy to have found this place to learn to program with everything so well explained and much more. But what I liked the most is that it has a place where I can ask questions and have the exercises reviewed, to see if the way to reach the result is correct, and in the case of not arriving, how would this solution be. So I’ll be showing you as many exercises as I can solve, I hope you don’t mind. What if I do not know if the doubts or reviews if you put them all in the same thread or open a thread for each exercise or topics there, you will tell me how to do it. Anyway, I leave you the solution for the next exercise to see if it turned out well.

## [Strings 3](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Strings#strings_3)

In the next string task, you are given the same quote that you ended up with in the previous task, but it is somewhat broken! We want you to fix and update it, like so:

1. Change the casing to correct sentence case (all lowercase, except for upper case first letter). Store the new quote in a variable called `fixedQuote` .
2. In `fixedQuote` , replace “green eggs and ham” with another food that you really don’t like.
3. There is one more small fix to do — add a full stop onto the end of the quote, and save the final version in a variable called `finalQuote` .

Try updating the live code below to recreate the finished example:

See the Pen [JavaScript Ejercicio 3: Cadena de Texto](https://codepen.io/ltakisl/pen/QWgYeyO) by lTakisl ([@ltakisl](https://codepen.io/ltakisl)) on [CodePen](https://codepen.io).

and above all thank you very much for your time, patience and dedication so that people like me can learn. 😀 😀 😀 😀 😀

---

<div class="post-metadata">

**Author:** ![mikoMK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mikomk/32/50456_2.png) [@mikoMK](https://discourse.mozilla.org/u/mikoMK)\
**Post date:** [September 30, 2021, 12:42am UTC](https://discourse.mozilla.org/t/javascript-strings/86498/6 "2021-09-30T00:42:34Z")

</div>

Thank you for the kind words. It means a lot to me to hear that my feedback helps you on your developer journey. 😊  
At the moment we are two volunteers who try our best to give helpful feedback. You can ask questions and show your exercises as much as you like. We are happy to help.

I would say posting tasks from the same topic in the same thread is okay (like you did here). When you are doing tasks in a new topic like “Arrays” or the “Silly story generator” assessment then you should create a new topic here. Pasting the instructions isn’t necessary the link to the task page itself is already fine.

Now let’s have a look at your “Strings 3” code:

Congratulation, that’s correct! 🏅  
When you `slice` to the end you can just omit the second parameter.

```auto
let indexLowerCase = quote.slice(1);

```

This does the same as your code.

Another alternative to correct the casing would be:

```auto
fixedQuote = quote[0].toUpperCase() + quote.slice(1).toLowerCase();

```

This way we don’t have to hardcode “I” into our code. It doesn’t matter what the first letter is.

Have a nice day!  
Michael

---

<div class="post-metadata">

**Author:** ![Takis](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/takis/32/51892_2.png) [@Takis](https://discourse.mozilla.org/u/Takis)\
**Post date:** [October 4, 2021, 1:22am UTC](https://discourse.mozilla.org/t/javascript-strings/86498/7 "2021-10-04T01:22:41Z")

</div>

Review and explanation of points 3 and 4 of the arrays of exercise 3

> **[Test your skills: Arrays - Learn web development | MDN](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Test_your_skills:_Arrays)**
>
> The aim of this skill test is to assess whether you've understood our Arrays article.

See the Pen [JavaScripts Ejercicio : 1 Matrices](https://codepen.io/ltakisl/pen/KKqLmOL) by lTakisl

See the Pen [JavaScripts Ejercicio : 2 Matrices](https://codepen.io/ltakisl/pen/Pojvjby) by lTakisl

See the Pen [JavaScripts Ejercicio : 3 Matrices](https://codepen.io/ltakisl/pen/KKqLvyq) by lTakisl

---

<div class="post-metadata">

**Author:** ![justsomeone](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/justsomeone/32/67787_2.png) [@justsomeone](https://discourse.mozilla.org/u/justsomeone)\
**Post date:** [October 4, 2021, 3:23pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498/8 "2021-10-04T15:23:39Z")

</div>

Hello @Takis

sorry but @mikoMK is not available right now but he will be back after while

so i will try my best

1. for array 1 and 2 you doing great well done
2. for array 3 you need to change each element in the array that mean you need to loop inside the array so you would need a loop statement you can use whatever you like for /while/do while there was for loop in the array lesson so i guess you can use it (let me know if i should explain it)

then for each element array[i] you need to change it’s content to the exact same content and add it’s index surrounded by () so you can do that inside your for loop like this

```auto
array[i]=array[i] + " ("+i+")" // i will be the counter in the for loop 
//since array[i] has string it will be concatenated with the (i) then it will assign the result back to array[i] 

```

after doing that you have the array you want so you can use the join method as you did

it would be better when you have new question you create new post unless it related to the same topic

hope that help and have a nice day 🙂

---

<div class="post-metadata">

**Author:** ![Takis](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/takis/32/51892_2.png) [@Takis](https://discourse.mozilla.org/u/Takis)\
**Post date:** [October 4, 2021, 7:16pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498/9 "2021-10-04T19:16:42Z")

</div>

That 's ok?

See the Pen [JavaScripts Ejercicio : 3 Matrices](https://codepen.io/ltakisl/pen/GREbRZx) by lTakisl ([@ltakisl](https://codepen.io/ltakisl)) on [CodePen](https://codepen.io).

---

<div class="post-metadata">

**Author:** ![justsomeone](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/justsomeone/32/67787_2.png) [@justsomeone](https://discourse.mozilla.org/u/justsomeone)\
**Post date:** [October 4, 2021, 7:32pm UTC](https://discourse.mozilla.org/t/javascript-strings/86498/10 "2021-10-04T19:32:30Z")

</div>

well done @Takis

just little note that :

1. missed semi colon at the end of the statement inside the for loop body
2. no need for the semi colon after the `}` of the for body
3. used your old solution when you used the join method before to join the array element the one you write in the comment there earlier

and have a nice day 🙂
