Files

43 lines
1.3 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Create Strings Using Template Literals
---
# Create Strings Using Template Literals
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
Instead of using string concatenation, ES6 offers template literals to create strings. In this challenge, you have to use template literals to create an array of text warnings.
It's required to use template literals to return a list as every element in the array as the element will be wrapped in a `<li></li>` tag.
2018-10-12 15:37:13 -04:00
---
## Hints
2018-10-12 15:37:13 -04:00
### Hint 1
2018-10-12 15:37:13 -04:00
* Use `map()` function to apply the template literals on all of the `arr` elements
### Hint 2
2018-10-12 15:37:13 -04:00
* Inside the `map()` use an arrow function which has `element` as a parameter and returns `<li></li>` that has the text-warning class and containing the `element` inside it
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
const resultDisplayArray = arr.map(item => `<li class="text-warning">${item}</li>`);
```
</details>
2018-10-12 15:37:13 -04:00
<details><summary>Solution 2 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
No map() solution
Despite being a less flexible solution, if you know the number of elements in advance, you can enumerate them as in
2018-10-12 15:37:13 -04:00
```javascript
const resultDisplayArray = [`<li class="text-warning">${arr[0]}</li>`,
2018-10-12 15:37:13 -04:00
`<li class="text-warning">${arr[1]}</li>`
,`<li class="text-warning">${arr[2]}</li>`];
```
</details>