2018-09-30 23:01:58 +01:00
---
id: 587d7b8a367417b2b2512b4e
title: Create Strings using Template Literals
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301200
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
A new feature of ES6 is the < dfn > template literal< / dfn > . This is a special type of string that makes creating complex strings easier.
Template literals allow you to create multi-line strings and to use string interpolation features to create strings.
Consider the code below:
2019-05-17 06:20:30 -07: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.
```
2018-09-30 23:01:58 +01:00
A lot of things happened there.
Firstly, the example uses backticks (< code > `< / code > ), not quotes (< code > '< / code > or < code > "< / code > ), to wrap the string.
Secondly, notice that the string is multi-line, both in the code and the output. This saves inserting < code > \n</ code > within strings.
The < code > ${variable}< / code > syntax used above is a placeholder. Basically, you won't have to use concatenation with the < code > +< / code > operator anymore. To add variables to strings, you just drop the variable in a template string and wrap it with < code > ${< / code > and < code > }< / code > . Similarly, you can include other expressions in your string literal, for example < code > ${a + b}< / code > .
This new way of creating strings gives you more flexibility to create robust strings.
< / section >
## Instructions
< section id = 'instructions' >
Use template literal syntax with backticks to display each entry of the < code > result< / code > object's < code > failure< / code > array. Each entry should be wrapped inside an < code > li< / code > element with the class attribute < code > text-warning< / code > , and listed within the < code > resultDisplayArray< / code > .
2018-11-22 22:21:51 +05:30
Use an iterator method (any kind of loop) to get the desired output.
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-07-24 01:47:32 -07:00
- text: < code > resultDisplayArray</ code > should be an array containing < code > result failure</ code > messages.
testString: assert(typeof makeList(result.failure) === 'object' & & resultDisplayArray.length === 3);
2019-11-27 02:57:38 -08:00
- text: < code > resultDisplayArray</ code > should be equal to the specified output.
2019-07-24 01:47:32 -07:00
testString: 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>` ));
2019-11-27 02:57:38 -08:00
- text: Template strings and expression interpolation should be used.
2019-07-24 01:47:32 -07:00
testString: getUserInput => assert(getUserInput('index').match(/(`.*\${.*}.*` )/));
2019-11-27 02:57:38 -08:00
- text: An iterator should be used.
2019-07-24 01:47:32 -07:00
testString: getUserInput => assert(getUserInput('index').match(/for|map|reduce|forEach|while/));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["id-blacklist", "no-dup-keys"]
};
function makeList(arr) {
"use strict";
// change code below this line
const resultDisplayArray = null;
// change code above this line
return resultDisplayArray;
}
/**
* makeList(result.failure) should return:
* [ `<li class="text-warning">no-var</li>` ,
2018-10-08 01:01:53 +01:00
* `<li class="text-warning">var-on-top</li>` ,
2018-09-30 23:01:58 +01:00
* `<li class="text-warning">linebreak</li>` ]
** /
const resultDisplayArray = makeList(result.failure);
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2018-11-22 22:21:51 +05:30
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["id-blacklist", "no-dup-keys"]
};
function makeList(arr) {
"use strict";
const resultDisplayArray = arr.map(val => `<li class="text-warning">${val}</li>` );
return resultDisplayArray;
}
/**
* makeList(result.failure) should return:
* [ `<li class="text-warning">no-var</li>` ,
* `<li class="text-warning">var-on-top</li>` ,
* `<li class="text-warning">linebreak</li>` ]
** /
const resultDisplayArray = makeList(result.failure);
2018-09-30 23:01:58 +01:00
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
< / section >