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

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.

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

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

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.