1.7 KiB
title |
---|
Create Strings Using Template Literals |
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.
Remember to use Read-Search-Ask
if you get stuck. Try to pair program and write your own code.
Problem Explanation:
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.
Hint: 1
- Use
map()
function to apply the template literals on all of thearr
elements
try to solve the problem now
Hint: 2
- Inside the
map()
use an arrow function which haselement
as a parameter and returns<li></li>
that has the text-warning class and containing theelement
inside it
try to solve the problem now
Spoiler Alert!
Solution ahead!
const resultDisplayArray = arr.map(item => `<li class="text-warning">${item}</li>`);
No map() solution
Despite being a less flexible solution, if you know the number of elements in advance, you can enumerate them as in
const resultDisplayArray = [`<li class="text-warning">${arr[0]}</li>`, `<li class="text-warning">${arr[1]}</li>` ,`<li class="text-warning">${arr[2]}</li>`];