MDN First steps: Strings 3

Hi,

I’d like to know if there is a simpler way to complete the task in 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

Much appreciated
Thom

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

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

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 article, the Skills Test: Strings for the next 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

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

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

Here’s my example. As you can see, it’s a little different to give a little different result… :stuck_out_tongue:
(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.

1 Like

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 in a day or two from now.
May 6, 2020

1 Like

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

1 Like

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.

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

1 Like

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

1 Like

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

@Olixpin you welcome and you will for sure :slight_smile:

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

Here is my coding answer below which can also be accessed at JS Fiddle: 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. :slightly_smiling_face:

1 Like

Hello @Dom67

you doing great

and have a nice day :slight_smile:

1 Like

Thank you so much! :slight_smile:

1 Like

you very welcome :slight_smile:

1 Like

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

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

Here was my first solution before comparing my answers:


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

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:


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


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

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

let finalQuote = `${fixedQuote}.`;