ES6: Create Strings using Template Literal

The instructions of the lesson are pretty clear. Each time through the loop, you need to output another string literal. A string literal is one which has spaces within the string that are made up of a javascript expression, of whatever sort, in-line. For example:

const pets = {
  dogs: ['Fido','Benji','Scraps'],
  cats: ['Fluffy','Pepper','Maximillian Manx'],
  others: ['Slither','Hedgie','Frank']
};

// If I want to loop over and print the names of the dogs...
let myDogsNames = [];

for (let i=0; i<pets.dogs.length; i++){
  // This line, right below, is the string literal. It will evaluate the expression 
  //    pets.dogs[i], and insert the value of that in place. We aren't saving it to
  //    a variable, as I simply want to add it to my dogs names array!
  myDogsNames.push( `<p>There is a doggie named ${ pets.dogs[i] }.</p>`);
}

// At this point, we went through all my doggies, and we have an array of all their names.
return myDogsNames;

What you’re trying to do is very similar, though the string literal should be a bit different.

1 Like