Test your skills: Strings
The aim of this skill test is to help you assess whether you've understood our Handling text — strings in JavaScript and Useful string methods articles.
Note: To get help, read our Test your skills usage guide. You can also reach out to us using one of our communication channels.
Strings 1
In our first strings task, we start off small. You already have half of a famous quote inside a variable called quoteStart
, and we want you to complete it.
To complete the task:
- Look up the other half of the quote, and add it to the example inside a variable called
quoteEnd
. - Concatenate the two strings together to make a single string containing the complete quote. Save the result inside a variable called
finalQuote
. - You'll find that you get an error at this point. Can you fix the problem with
quoteStart
, so that the full quote displays correctly?
const quoteStart = 'Don't judge each day by the harvest you reap ';
// Add your code here
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
para1.textContent = finalQuote;
section.appendChild(para1);
Click here to show the solution
Your finished JavaScript should look something like this:
// You need to escape the quote
const quoteStart = 'Don\'t judge each day by the harvest you reap ';
const quoteEnd = "but by the seeds that you plant.";
const finalQuote = `${quoteStart}${quoteEnd}`;
// Don't edit the code below here!
// ...
Strings 2
In this task you are provided with two variables, quote
and substring
, which contain two strings.
To complete the task:
- Retrieve the length of the quote, and store it in a variable called
quoteLength
. - Find the index position where
substring
appears inquote
, and store that value in a variable calledindex
. - 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
.
const quote = "I do not like green eggs and ham. I do not like them, Sam-I-Am.";
const substring = "green eggs and ham";
// Don't edit the code above here!
// Add your code here
// Don't edit the code below here!
const section = document.querySelector("section");
section.innerHTML = " ";
const para1 = document.createElement("p");
para1.textContent = `The quote is ${quoteLength} characters long.`;
const para2 = document.createElement("p");
para2.textContent = revisedQuote;
section.appendChild(para1);
section.appendChild(para2);
Click here to show the solution
Your finished JavaScript should look something like this:
// ...
// Don't edit the code above here!
const quoteLength = quote.length;
const index = quote.indexOf(substring);
const revisedQuote = quote.slice(0, index + substring.length + 1);
// Don't edit the code below here!
// ...
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.
To complete the task:
- Change the casing to correct sentence case (all lowercase, except for upper case first letter). Store the new quote in a variable called
fixedQuote
. - In
fixedQuote
, replace "green eggs and ham" with another food that you really don't like. - There is one more small fix to do — add a period to the end of the quote, and save the final version in a variable called
finalQuote
.
const quote = "I dO nOT lIke gREen eGgS anD HAM";
// Don't edit the code above here!
// Add your code here
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
para1.textContent = finalQuote;
section.appendChild(para1);
Click here to show the solution
Your finished JavaScript should look something like this:
// ...
// Don't edit the code above here!
let fixedQuote = quote.toLowerCase();
const firstLetter = fixedQuote.slice(0, 1);
fixedQuote = fixedQuote.replace(firstLetter, firstLetter.toUpperCase());
fixedQuote = fixedQuote.replace("green eggs and ham", "pickled onions");
const finalQuote = `${fixedQuote}.`;
// Don't edit the code below here!
// ...
Strings 4
In the final string task, we have given you the name of a theorem, two numeric values, and an incomplete string (the bits that need adding are marked with asterisks (*
)). We want you to change the value of the string.
To complete the task:
- Change the string from a regular string literal into a template literal.
- Replace the four asterisks with four template literal embedded expressions. These should be:
- The name of the theorem.
- The two number values we have.
- The length of the hypotenuse of a right-angled triangle, assuming the two other side lengths are the same as the two values we have. You'll need to look up how to calculate this from what you have. Do the calculation inside the placeholder.
const theorem = "Pythagorean theorem";
const a = 5;
const b = 8;
// Don't edit the code above here!
// Edit the string literal
const myString =
"Using *, we can work out that if the two shortest sides of a right-angled triangle have lengths of * and *, the length of the hypotenuse is *.";
// Don't edit the code below here!
const section = document.querySelector("section");
const para1 = document.createElement("p");
para1.textContent = myString;
section.appendChild(para1);
Click here to show the solution
Your finished JavaScript should look something like this:
// ...
// Don't edit the code above here!
const myString = `Using ${theorem}, we can work out that if the two shortest sides of a right-angled triangle have lengths of ${a} and ${b},
the length of the hypotenuse is ${Math.sqrt(a ** 2 + b ** 2)}.`;
// Don't edit the code below here!
// ...