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
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.
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
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… (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');
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:
Change the casing to correct sentence case (all lowercase, except for upper case first letter). Store the new quote in a variable calledfixedQuote (edited: Store this new quote in a new variable calledfixedQuote).
InfixedQuote , 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 + '!';
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?
// 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;
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
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}.`;