-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
46 lines (34 loc) · 1.09 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 57: Default parameters - basics (solution)
// To do: make all tests pass, leave the assert lines unchanged!
describe('default parameters make function parameters more flexible', () => {
it('define it using an assignment to the parameter `function(param=1){}`', function() {
let number = (int = 0) => int;
assert.equal(number(), 0);
});
it('it is used when undefined is passed', function() {
let number = (int = 23) => int;
const param = undefined;
assert.equal(number(param), 23);
});
it('it is not used when a value is given', function() {
function xhr(method = 'GET') {
return method;
}
assert.equal(xhr('POST'), 'POST');
});
it('it is evaluated at run time', function() {
let defaultValue = 42;
function xhr(method = `value: ${defaultValue}`) {
return method;
}
assert.equal(xhr(), 'value: 42');
defaultValue = 23;
});
it('it can also be a function', function() {
let defaultValue = () => {}
function fn(value = defaultValue()) {
return value;
}
assert.equal(fn(), defaultValue());
});
});