2018-09-30 22:01:58 +00:00
---
id: 587d7b8a367417b2b2512b4e
title: Create Strings using Template Literals
challengeType: 1
2019-08-05 16:17:33 +00:00
forumTopicId: 301200
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2018-09-30 22:01:58 +00:00
A new feature of ES6 is the < dfn > template literal< / dfn > . This is a special type of string that makes creating complex strings easier.
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
Template literals allow you to create multi-line strings and to use string interpolation features to create strings.
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
Consider the code below:
2019-05-17 13:20:30 +00:00
```js
const person = {
name: "Zodiac Hasbro",
age: 56
};
// Template literal with multi-line and string interpolation
const greeting = `Hello, my name is ${person.name}!
I am ${person.age} years old.`;
console.log(greeting); // prints
// Hello, my name is Zodiac Hasbro!
// I am 56 years old.
```
2020-11-27 18:02:05 +00:00
A lot of things happened there. Firstly, the example uses backticks (`` ` ``), not quotes (` '` or `"` ), to wrap the string. Secondly, notice that the string is multi-line, both in the code and the output. This saves inserting `\n` within strings. The `${variable}` syntax used above is a placeholder. Basically, you won't have to use concatenation with the `+` operator anymore. To add variables to strings, you just drop the variable in a template string and wrap it with `${` and `}` . Similarly, you can include other expressions in your string literal, for example `${a + b}` . This new way of creating strings gives you more flexibility to create robust strings.
# --instructions--
Use template literal syntax with backticks to create an array of list element (`li`) strings. Each list element's text should be one of the array elements from the `failure` property on the `result` object and have a `class` attribute with the value `text-warning` . The `makeList` function should return the array of list item strings.
2018-09-30 22:01:58 +00:00
2020-03-04 19:08:54 +00:00
Use an iterator method (any kind of loop) to get the desired output (shown below).
```js
[
'< li class = "text-warning" > no-var< / li > ',
'< li class = "text-warning" > var-on-top< / li > ',
'< li class = "text-warning" > linebreak< / li > '
]
```
2020-11-27 18:02:05 +00:00
# --hints--
`failuresList` should be an array containing `result failure` messages.
```js
assert(
typeof makeList(result.failure) === 'object' & & failuresList.length === 3
);
```
`failuresList` should be equal to the specified output.
```js
assert(
makeList(result.failure).every(
(v, i) =>
v === `<li class="text-warning">${result.failure[i]}</li>` ||
v === `<li class='text-warning'>${result.failure[i]}</li>`
)
);
```
Template strings and expression interpolation should be used.
```js
(getUserInput) => assert(getUserInput('index').match(/(`.*\${.*}.*`)/));
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
An iterator should be used.
```js
(getUserInput) =>
assert(getUserInput('index').match(/for|map|reduce|forEach|while/));
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
2020-09-07 05:34:44 +00:00
skipped: ["no-extra-semi", "no-dup-keys"]
2018-09-30 22:01:58 +00:00
};
function makeList(arr) {
2020-03-04 19:08:54 +00:00
// Only change code below this line
2020-09-10 00:58:27 +00:00
const failureItems = [];
2020-03-04 19:08:54 +00:00
// Only change code above this line
2018-09-30 22:01:58 +00:00
2020-09-10 00:58:27 +00:00
return failureItems;
2018-09-30 22:01:58 +00:00
}
2020-03-04 19:08:54 +00:00
2020-09-10 00:58:27 +00:00
const failuresList = makeList(result.failure);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2018-11-22 16:51:51 +00:00
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
2020-09-07 05:34:44 +00:00
skipped: ["no-extra-semi", "no-dup-keys"]
2018-11-22 16:51:51 +00:00
};
function makeList(arr) {
2020-09-10 00:58:27 +00:00
return arr.map(val => `<li class="text-warning">${val}</li>` );
2018-11-22 16:51:51 +00:00
}
2020-03-04 19:08:54 +00:00
2020-09-10 00:58:27 +00:00
const failuresList = makeList(result.failure);
2018-09-30 22:01:58 +00:00
```