JavaScript Strings

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

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);

Hi @Takis and welcome to the community :wave:

  1. Correct :white_check_mark:
  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

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

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 by lTakisl (@ltakisl) on CodePen.

1 Like

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 :grin:
You can calculate endIndex like this:

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

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

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 by lTakisl (@ltakisl) on CodePen.

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

1 Like

Thank you for the kind words. It means a lot to me to hear that my feedback helps you on your developer journey. :blush:
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! :medal_sports:
When you slice to the end you can just omit the second parameter.

let indexLowerCase = quote.slice(1);

This does the same as your code.

Another alternative to correct the casing would be:

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

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

See the Pen JavaScripts Ejercicio : 1 Matrices by lTakisl

See the Pen JavaScripts Ejercicio : 2 Matrices by lTakisl

See the Pen JavaScripts Ejercicio : 3 Matrices by lTakisl

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

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 :slight_smile:

1 Like

That 's ok?

See the Pen JavaScripts Ejercicio : 3 Matrices by lTakisl (@ltakisl) on CodePen.

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 :slight_smile:

1 Like