.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
compare-scopes-of-the-var-and-let-keywords.english.md
create-an-export-fallback-with-export-default.english.md
create-strings-using-template-literals.english.md
declare-a-read-only-variable-with-the-const-keyword.english.md
explore-differences-between-the-var-and-let-keywords.english.md
import-a-default-export.english.md
mutate-an-array-declared-with-const.english.md
prevent-object-mutation.english.md
set-default-parameters-for-your-functions.english.md
understand-the-differences-between-import-and-require.english.md
use--to-import-everything-from-a-file.english.md
use-arrow-functions-to-write-concise-anonymous-functions.english.md
use-class-syntax-to-define-a-constructor-function.english.md
use-destructuring-assignment-to-assign-variables-from-arrays.english.md
use-destructuring-assignment-to-assign-variables-from-nested-objects.english.md
use-destructuring-assignment-to-assign-variables-from-objects.english.md
use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.english.md
use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements.english.md
use-export-to-reuse-a-code-block.english.md
use-getters-and-setters-to-control-access-to-an-object.english.md
use-the-rest-operator-with-function-parameters.english.md
use-the-spread-operator-to-evaluate-arrays-in-place.english.md
write-arrow-functions-with-parameters.english.md
write-concise-declarative-functions-with-es6.english.md
write-concise-object-literal-declarations-using-simple-fields.english.md
write-higher-order-arrow-functions.english.md
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
portuguese
russian
spanish
adddir.js
translatemd.js
formattingConversion
math-challenges
requiresTests
schema
.babelrc
.editorconfig
.eslintignore
.eslintrc
.npmignore
.prettierrc
.travis.yml
CHANGELOG.md
LICENSE.md
addAssertsToTapTest.js
challengeTitles.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
index.js
lib.js
md-translation.js
mongoIds.js
package-entry.js
package-lock.json
package.json
repack.js
test-challenges.js
unpack.js
unpacked.css
unpacked.js
unpackedChallenge.js
utils.js
docs
news
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env
69 lines
2.7 KiB
Markdown
69 lines
2.7 KiB
Markdown
---
|
|
id: 587d7b87367417b2b2512b43
|
|
title: Use Arrow Functions to Write Concise Anonymous Functions
|
|
challengeType: 1
|
|
---
|
|
|
|
## Description
|
|
<section id='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:
|
|
<blockquote>const myFunc = function() {<br> const myVar = "value";<br> return myVar;<br>}</blockquote>
|
|
ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use <strong>arrow function syntax</strong>:
|
|
<blockquote>const myFunc = () => {<br> const myVar = "value";<br> return myVar;<br>}</blockquote>
|
|
When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword <code>return</code> as well as the brackets surrounding the code. This helps simplify smaller functions into one-line statements:
|
|
<blockquote>const myFunc = () => "value"</blockquote>
|
|
This code will still return <code>value</code> by default.
|
|
</section>
|
|
|
|
## Instructions
|
|
<section id='instructions'>
|
|
Rewrite the function assigned to the variable <code>magic</code> which returns a new <code>Date()</code> to use arrow function syntax. Also make sure nothing is defined using the keyword <code>var</code>.
|
|
</section>
|
|
|
|
## Tests
|
|
<section id='tests'>
|
|
|
|
```yml
|
|
tests:
|
|
- text: User did replace <code>var</code> keyword.
|
|
testString: 'getUserInput => assert(!getUserInput("index").match(/var/g), "User did replace <code>var</code> keyword.");'
|
|
- text: <code>magic</code> should be a constant variable (by using <code>const</code>).
|
|
testString: 'getUserInput => assert(getUserInput("index").match(/const\s+magic/g), "<code>magic</code> should be a constant variable (by using <code>const</code>).");'
|
|
- text: <code>magic</code> is a <code>function</code>.
|
|
testString: 'assert(typeof magic === "function", "<code>magic</code> is a <code>function</code>.");'
|
|
- text: <code>magic()</code> returns correct date.
|
|
testString: 'assert(magic().getDate() == new Date().getDate(), "<code>magic()</code> returns correct date.");'
|
|
- text: <code>function</code> keyword was not used.
|
|
testString: 'getUserInput => assert(!getUserInput("index").match(/function/g), "<code>function</code> keyword was not used.");'
|
|
|
|
```
|
|
|
|
</section>
|
|
|
|
## Challenge Seed
|
|
<section id='challengeSeed'>
|
|
|
|
<div id='js-seed'>
|
|
|
|
```js
|
|
var magic = function() {
|
|
"use strict";
|
|
return new Date();
|
|
};
|
|
```
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
## Solution
|
|
<section id='solution'>
|
|
|
|
```js
|
|
// solution required
|
|
```
|
|
</section>
|