# MDN First steps: Strings 3

**URL:** <https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516>\
**Category:** Learn\
**Created:** [February 22, 2020, 9:06am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516 "2020-02-22T09:06:38Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomjavare](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/thomjavare/32/34485_2.png) [@thomjavare](https://discourse.mozilla.org/u/thomjavare)\
**Post date:** [February 22, 2020, 9:06am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/1 "2020-02-22T09:06:38Z")

</div>

Hi,

I’d like to know if there is a simpler way to complete the task in [Strings 3](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Strings#Strings_3)

I mean I’m getting the desired outcome (I think) but is it correct?

It seems like it required code which wasn’t really covered yet? Is this the norm?

[CodePen](https://codepen.io/thomjavare/pen/OJVbOev)

Much appreciated  
Thom

---

<div class="post-metadata">

**Author:** ![urty5656](https://avatars.discourse-cdn.com/v4/letter/u/45deac/32.png) [@urty5656](https://discourse.mozilla.org/u/urty5656)\
**Post date:** [February 24, 2020, 7:35am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/2 "2020-02-24T07:35:45Z")

</div>

@thomjavare

`charAt(0)` and `substr()` isn’t covered, but `[0]` and `slice()` is. Neither of them are wrong, of course.

Go visit the [Useful string methods](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods#Finding_a_substring_inside_a_string_and_extracting_it) page, and take a look at the “Retrieving a specific string character” and “Finding a substring inside a string and extracting it” sections.

Also, try not to reassign the `quote` variable. That is, the line number 4:

```auto
let quote = 'I dO nOT lIke gREen eGgS anD HAM';

// Add your code here
quote = 'I do not like green eggs and ham'; // Try to complete without this

```

---

<div class="post-metadata">

**Author:** ![thomjavare](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/thomjavare/32/34485_2.png) [@thomjavare](https://discourse.mozilla.org/u/thomjavare)\
**Post date:** [February 24, 2020, 10:48am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/3 "2020-02-24T10:48:12Z")

</div>

Hi @urty5656,

Thank you very much for the feedback, much appreciated.

Just FYI, I think I know where the confusion came in…

On the [Handling text](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Strings) article, the [Skills Test: Strings](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Strings) for the [next](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods) article you mentioned above is also on the Handling text article under “Test your Skills”.

Thus I only learned about the `[0]` and `slice()`when I moved on to the Useful String methods article.

Again, thanks for your response.  
Thomas

---

<div class="post-metadata">

**Author:** ![rodrigoguima](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/rodrigoguima/32/35783_2.png) [@rodrigoguima](https://discourse.mozilla.org/u/rodrigoguima)\
**Post date:** [March 30, 2020, 4:16pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/4 "2020-03-30T16:16:18Z")

</div>

Hey! I just finished my code and I was looking for some results and I found your question here. It looks a bit different to yours but also works fine 🙂

```auto
  let quote = 'I dO nOT lIke gREen eGgS anD HAM';

// Add your code here

let lower = quote.toLowerCase();
let first = lower[0];
let cap = lower.replace(first, first.toUpperCase());
let fixedQuote = cap.replace('green eggs and ham', 'coriander');
let finalQuote = fixedQuote + '.';

// Don't edit the code below here!

section.innerHTML = ' ';
let para1 = document.createElement('p');
para1.textContent = fixedQuote;
let para2 = document.createElement('p');
para2.textContent = finalQuote;

section.appendChild(para1);
section.appendChild(para2);

```

---

<div class="post-metadata">

**Author:** ![DigitalEngine](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/digitalengine/32/34430_2.png) [@DigitalEngine](https://discourse.mozilla.org/u/DigitalEngine)\
**Post date:** [May 4, 2020, 10:54pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/5 "2020-05-04T22:54:34Z")

</div>

Here’s my example. As you can see, it’s a little different to give a little different result… 😛  
_(all the task objectives are still contained in this example, just in a slightly different order)_.

```
let quoteToLowerCase = quote.toLowerCase();
let firstLetter = quoteToLowerCase[0];
let preFixedQuote = firstLetter.toUpperCase() + quoteToLowerCase.slice(1) + '!';

let fixedQuote = 'Sam says, "' + preFixedQuote + '"'

let finalQuote = 'But, ' + preFixedQuote.replace('green eggs and ham','moldy cheese and green beets');

```

Working example is [here at my testing and learning github site](https://risingthunder.github.io/code/java-projects/strings3.html).

---

<div class="post-metadata">

**Author:** ![DigitalEngine](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/digitalengine/32/34430_2.png) [@DigitalEngine](https://discourse.mozilla.org/u/DigitalEngine)\
**Post date:** [May 7, 2020, 12:24am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/6 "2020-05-07T00:24:20Z")

</div>

After doing this exercise a second time, I realized that the `.replace` method can be used more than once on the same line. As such, I’m thinking that this is what the exercise is actually looking for.  
As the directions indicate:

> 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`  
> _(edited: Store this **new quote** in a new variable **called** _ `fixedQuote`).
> 2. **In** `fixedQuote` , replace “green eggs and ham” with another food that you really don’t like.

```
let lowerCase = quote.toLowerCase();
let firstChar = lowerCase[0];
let fixedQuote = lowerCase.replace(firstChar,firstChar.toUpperCase()).replace('green eggs and ham','moldy cheese and green beets');
let finalQuote = fixedQuote + '!';

```

I’ll post a full working example of this code [here at my testing and learning github site](https://risingthunder.github.io/code/java-projects/strings3.html) in a day or two from now.  
May 6, 2020

---

<div class="post-metadata">

**Author:** ![Jose\_C](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jose\_C](https://discourse.mozilla.org/u/Jose_C)\
**Post date:** [May 22, 2020, 5:27pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/7 "2020-05-22T17:27:36Z")

</div>

Hey guys, This is my example of the code:

let quote = ‘I dO nOT lIke gREen eGgS anD HAM’;

// Add your code here  
let fixedQuote = quote[0].toUpperCase() + quote.slice(1).toLowerCase();  
finalQuote= fixedQuote.replace(‘green eggs and ham’, ‘rice’) + ‘.’;  
// Don’t edit the code below here!

section.innerHTML = ’ ';  
let para1 = document.createElement(‘p’);  
para1.textContent = fixedQuote;  
let para2 = document.createElement(‘p’);  
para2.textContent = finalQuote;

section.appendChild(para1);  
section.appendChild(para2);

---

<div class="post-metadata">

**Author:** ![chrisdavidmills](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/chrisdavidmills/32/21688_2.png) [@chrisdavidmills](https://discourse.mozilla.org/u/chrisdavidmills)\
**Post date:** [May 25, 2020, 9:37am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/8 "2020-05-25T09:37:45Z")

</div>

Hi @Jose_C! Welcome to our community, and thanks for sending your code in.

I’ve tested it, and it works absolutely fine — well done on some good work.

---

<div class="post-metadata">

**Author:** ![Olixpin](https://avatars.discourse-cdn.com/v4/letter/o/dec6dc/32.png) [@Olixpin](https://discourse.mozilla.org/u/Olixpin)\
**Post date:** [September 23, 2020, 8:54pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/9 "2020-09-23T20:54:17Z")

</div>

Hello guys, this is my own solution and it seems totally different from you guys. What do you think about it?

I am taking my time to study you guys code so I can learn.  
Your feedback will be appreciated. Thanks!

let quote = ‘I dO nOT lIke gREen eGgS anD HAM’;

// Add your code here  
let fixedQuote = quote.slice(0, 1) + ’ ’ + quote.slice(2).toLowerCase();  
fixedQuote = fixedQuote.replace(“green eggs and ham”, “sweet potato”)

let finalQuote = fixedQuote + “.”;

// Don’t edit the code below here!

section.innerHTML = ’ ';  
let para1 = document.createElement(‘p’);  
para1.textContent = fixedQuote;  
let para2 = document.createElement(‘p’);  
para2.textContent = finalQuote;

section.appendChild(para1);  
section.appendChild(para2);

---

<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:** [September 24, 2020, 8:44am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/10 "2020-09-24T08:44:57Z")

</div>

Hello @Olixpin you doing great well done and have a nice day

---

<div class="post-metadata">

**Author:** ![Olixpin](https://avatars.discourse-cdn.com/v4/letter/o/dec6dc/32.png) [@Olixpin](https://discourse.mozilla.org/u/Olixpin)\
**Post date:** [September 27, 2020, 11:06pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/11 "2020-09-27T23:06:28Z")

</div>

Thank you. I hope I can be great developer one day! have been studying all night

---

<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:** [September 28, 2020, 2:38pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/12 "2020-09-28T14:38:37Z")

</div>

@Olixpin you welcome and you will for sure 🙂

---

<div class="post-metadata">

**Author:** ![Dom67](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/dom67/32/41553_2.png) [@Dom67](https://discourse.mozilla.org/u/Dom67)\
**Post date:** [September 30, 2020, 6:15pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/13 "2020-09-30T18:15:24Z")

</div>

Could I get a check on my coding to verify if it is the correct result?

Here is the link to Strings Test 3 for your convenience: [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First\_steps/Test\_your\_skills:\_Strings](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Test_your_skills:_Strings)

Here is my coding answer below which can also be accessed at JS Fiddle: [https://jsfiddle.net/Dom67/2oyfz38L/7/](https://jsfiddle.net/Dom67/2oyfz38L/7/)

let quote = ‘I dO nOT lIke gREen eGgS anD HAM’;  
let input = quote;  
let lower = input.toLowerCase();  
let firstLetter = lower.slice(0,1);  
let capitalized = lower.replace(firstLetter,firstLetter.toUpperCase());  
let fixedQuote = capitalized;  
let finalQuote = capitalized.replace(‘green eggs and ham’,‘sausage and beans’) + ‘.’;

The result I got at the “live update” field at the MDN “Test your skills: Strings” page (first url above) was as follows:

I do not like green eggs and ham

I do not like sausage and beans.

Is this the desired result? Do I need to change anything?

Thank you. 🙂

---

<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:** [September 30, 2020, 7:27pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/14 "2020-09-30T19:27:00Z")

</div>

Hello @Dom67

you doing great

and have a nice day 🙂

---

<div class="post-metadata">

**Author:** ![Dom67](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/dom67/32/41553_2.png) [@Dom67](https://discourse.mozilla.org/u/Dom67)\
**Post date:** [September 30, 2020, 10:37pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/15 "2020-09-30T22:37:29Z")

</div>

Thank you so much! 🙂

---

<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 1, 2020, 1:05am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/16 "2020-10-01T01:05:33Z")

</div>

you very welcome 🙂

---

<div class="post-metadata">

**Author:** ![Shah\_Hussain](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@Shah\_Hussain](https://discourse.mozilla.org/u/Shah_Hussain)\
**Post date:** [August 25, 2021, 9:16am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/17 "2021-08-25T09:16:22Z")

</div>

my code

let quote = ‘I dO nOT lIke gREen eGgS anD HAM’;

// Add your code here  
let fixedQuote=quote.charAt(0).toUpperCase() + quote.slice(1).toLowerCase() ;  
fixedQuote= fixedQuote.replace(“green eggs and ham”,“butter and chease”);

fixedQuote +="."

let finalQuote=fixedQuote;  
// Don’t edit the code below here!

section.innerHTML = ’ ';  
let para1 = document.createElement(‘p’);  
para1.textContent = fixedQuote;  
let para2 = document.createElement(‘p’);  
para2.textContent = finalQuote;

section.appendChild(para1);  
section.appendChild(para2);

---

<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:** [August 25, 2021, 12:00pm UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/18 "2021-08-25T12:00:34Z")

</div>

Hello @Shah_Hussain

you doing great well done

it would be better that you create your own post and put your code on any online service like [codepen.io](https://codepen.io) or [jsfiddle.net](https://jsfiddle.net/) and also share link to the topic or task you asking about so it be easier for everyone to help

and have a nice day 🙂

---

<div class="post-metadata">

**Author:** ![jt\_1](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/jt_1/32/62504_2.png) [@jt\_1](https://discourse.mozilla.org/u/jt_1)\
**Post date:** [August 29, 2023, 3:33am UTC](https://discourse.mozilla.org/t/mdn-first-steps-strings-3/54516/20 "2023-08-29T03:33:55Z")

</div>

**Here was my first solution before comparing my answers:**

```auto

const quote = "I dO nOT lIke gREen eGgS anD HAM";

```

```auto
let newCase = quote.toLowerCase();
let minusFirst = newCase.slice(1);
let firstLetter = quote[0].toUpperCase();

let fixedQuote = `${firstLetter}${minusFirst}`;

let fixedQuote = fixedQuote.replace("green eggs and ham", "peanut butter");

let finalQuote = `${fixedQuote}.`;

```

  

**But then after reading other solutions above, I prefer this solution:**

```auto

const quote = "I dO nOT lIke gREen eGgS anD HAM";

```

```auto

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

let fixedQuote = fixedQuote.replace("green eggs and ham", "peanut butter");

let finalQuote = `${fixedQuote}.`;

```
