* Put value into quotes It can be missunderstanding that "value" is a string! * fix: remove quotes from inside code tags Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
2.4 KiB
2.4 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7b87367417b2b2512b43 | Use Arrow Functions to Write Concise Anonymous Functions | 1 | 301211 |
Description
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
magic
which returns a new Date()
to use arrow function syntax. Also, make sure nothing is defined using the keyword var
.
Tests
tests:
- text: User should replace <code>var</code> keyword.
testString: getUserInput => assert(!getUserInput('index').match(/var/g));
- text: <code>magic</code> should be a constant variable (by using <code>const</code>).
testString: getUserInput => assert(getUserInput('index').match(/const\s+magic/g));
- text: <code>magic</code> should be a <code>function</code>.
testString: assert(typeof magic === 'function');
- text: <code>magic()</code> should return correct date.
testString: assert(magic().setHours(0,0,0,0) === new Date().setHours(0,0,0,0));
- text: <code>function</code> keyword should not be used.
testString: getUserInput => assert(!getUserInput('index').match(/function/g));
Challenge Seed
var magic = function() {
"use strict";
return new Date();
};
Solution
const magic = () => {
"use strict";
return new Date();
};