-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
95 lines (84 loc) · 1.78 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2023 by Shakhbozbek Usmonov Miracle Programmer.
// leetcode.com 30 Days of JavaScript challenge
// ----- Started -----
/**
* @return {Function}
*/
var createHelloWorld = function () {
return function (...args) {
return "Hello World";
};
};
// --------------------------------
/**
* const f = createHelloWorld();
* f(); // "Hello World"
*/
/**
* @param {number} n
* @return {Function} counter
*/
var createCounter = function (n) {
return function () {
return n++;
};
};
// --------------------------------
/**
* const counter = createCounter(10)
* counter() // 10
* counter() // 11
* counter() // 12
*/
/**
* @param {string} val
* @return {Object}
*/
var expect = function (val) {
return {
toBe: (otherVal) => {
if (val !== otherVal) {
throw new Error("Not Equal");
} else {
return true;
}
},
notToBe: (otherVal) => {
if (val === otherVal) {
throw new Error("Equal");
} else {
return true;
}
},
};
};
/**
* expect(5).toBe(5); // true
* expect(5).notToBe(5); // throws "Equal"
*/
//-------------------------------------------------
/**
* @param {integer} init
* @return { increment: Function, decrement: Function, reset: Function }
*/
var createCounter = function (init) {
var count = init;
return {
increment: () => {
return ++count;
},
decrement: () => {
return --count;
},
reset: () => {
count = init;
return count;
},
};
};
/**
* const counter = createCounter(5)
* counter.increment(); // 6
* counter.reset(); // 5
* counter.decrement(); // 4
*/