* fix(curriculum): tests quotes * fix(curriculum): fill seed-teardown * fix(curriculum): fix tests and remove unneeded seed-teardown
2.7 KiB
2.7 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d7b87367417b2b2512b43 | Use Arrow Functions to Write Concise Anonymous Functions | 1 |
Description
const myFunc = function() {ES6 provides us with the syntactic sugar to not have to write anonymous functions this way. Instead, you can use arrow function syntax:
const myVar = "value";
return myVar;
}
const myFunc = () => {When there is no function body, and only a return value, arrow function syntax allows you to omit the keyword
const myVar = "value";
return myVar;
}
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
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 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.');
Challenge Seed
var magic = function() {
"use strict";
return new Date();
};
Solution
const magic = () => {
"use strict";
return new Date();
};