* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.1 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b87367417b2b2512b43 | Use Arrow Functions to Write Concise Anonymous Functions | 1 | 301211 | use-arrow-functions-to-write-concise-anonymous-functions |
--description--
In JavaScript, we often don't need to name our functions, especially when passing a function as an argument to another function. Instead, we create inline functions. We don't need to name these functions because we do not reuse them anywhere else.
To achieve this, we often use the following syntax:
const myFunc = function() {
const myVar = "value";
return myVar;
}
ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use arrow function syntax:
const myFunc = () => {
const myVar = "value";
return myVar;
}
When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword return
as well as the brackets surrounding the code. This helps simplify smaller functions into one-line statements:
const myFunc = () => "value";
This code will still return the string value
by default.
--instructions--
Rewrite the function assigned to the variable magic
which returns a new Date()
to use arrow function syntax. Also, make sure nothing is defined using the keyword var
.
--hints--
User should replace var
keyword.
(getUserInput) => assert(!getUserInput('index').match(/var/g));
magic
should be a constant variable (by using const
).
(getUserInput) => assert(getUserInput('index').match(/const\s+magic/g));
magic
should be a function
.
assert(typeof magic === 'function');
magic()
should return correct date.
assert(magic().setHours(0, 0, 0, 0) === new Date().setHours(0, 0, 0, 0));
function
keyword should not be used.
(getUserInput) => assert(!getUserInput('index').match(/function/g));
--seed--
--seed-contents--
var magic = function() {
return new Date();
};
--solutions--
const magic = () => {
return new Date();
};