The existing terminology carries negative sentiment that can be interpreted in a racial or sense. Updating the name to have no potential for such a connection. Co-authored-by: Justin Rogers <justrog@gmail.com>
3.8 KiB
3.8 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7b8a367417b2b2512b4e | Create Strings using Template Literals | 1 | 301200 |
Description
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.
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
result
object's failure
array. Each entry should be wrapped inside an li
element with the class attribute text-warning
, and listed within the resultDisplayArray
.
Use an iterator method (any kind of loop) to get the desired output (shown below).
[
'<li class="text-warning">no-var</li>',
'<li class="text-warning">var-on-top</li>',
'<li class="text-warning">linebreak</li>'
]
Tests
tests:
- text: <code>resultDisplayArray</code> should be an array containing <code>result failure</code> messages.
testString: assert(typeof makeList(result.failure) === 'object' && resultDisplayArray.length === 3);
- text: <code>resultDisplayArray</code> should be equal to the specified output.
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>`));
- text: Template strings and expression interpolation should be used.
testString: getUserInput => assert(getUserInput('index').match(/(`.*\${.*}.*`)/));
- text: An iterator should be used.
testString: getUserInput => assert(getUserInput('index').match(/for|map|reduce|forEach|while/));
Challenge Seed
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["no-extra-semi", "no-dup-keys"]
};
function makeList(arr) {
"use strict";
// Only change code below this line
const resultDisplayArray = null;
// Only change code above this line
return resultDisplayArray;
}
const resultDisplayArray = makeList(result.failure);
Solution
const result = {
success: ["max-length", "no-amd", "prefer-arrow-functions"],
failure: ["no-var", "var-on-top", "linebreak"],
skipped: ["no-extra-semi", "no-dup-keys"]
};
function makeList(arr) {
"use strict";
const resultDisplayArray = arr.map(val => `<li class="text-warning">${val}</li>`);
return resultDisplayArray;
}
const resultDisplayArray = makeList(result.failure);