Skip to content

Commit f14bca7

Browse files
committed
2 more ejs
1 parent 269b419 commit f14bca7

File tree

3 files changed

+129
-13
lines changed

3 files changed

+129
-13
lines changed

30 days JS/14-SleepPromises.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Given a positive integer millis, write an asynchronous function that sleeps for millis milliseconds. It can resolve any value.
3+
4+
5+
6+
Example 1:
7+
8+
Input: millis = 100
9+
Output: 100
10+
Explanation: It should return a promise that resolves after 100ms.
11+
let t = Date.now();
12+
sleep(100).then(() => {
13+
console.log(Date.now() - t); // 100
14+
});
15+
Example 2:
16+
17+
Input: millis = 200
18+
Output: 200
19+
Explanation: It should return a promise that resolves after 200ms.
20+
21+
22+
Constraints:
23+
24+
1 <= millis <= 1000
25+
*/
26+
27+
/**
28+
* @param {number} millis
29+
* @return {Promise}
30+
*/
31+
function sleep(millis) {
32+
return new Promise((resolve) => setTimeout(resolve, millis));
33+
}

30 days JS/15-TimeOurCancelation.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Given a function fn, an array of arguments args, and a timeout t in milliseconds, return a cancel function cancelFn.
3+
4+
After a delay of t, fn should be called with args passed as parameters unless cancelFn was invoked before the delay of t milliseconds elapses, specifically at cancelT ms. In that case, fn should never be called.
5+
6+
7+
8+
Example 1:
9+
10+
Input: fn = (x) => x * 5, args = [2], t = 20, cancelT = 50
11+
Output: [{"time": 20, "returned": 10}]
12+
Explanation:
13+
const result = []
14+
15+
const fn = (x) => x * 5
16+
17+
const start = performance.now()
18+
19+
const log = (...argsArr) => {
20+
const diff = Math.floor(performance.now() - start);
21+
result.push({"time": diff, "returned": fn(...argsArr)})
22+
}
23+
24+
const cancel = cancellable(log, [2], 20);
25+
26+
const maxT = Math.max(t, 50)
27+
28+
setTimeout(cancel, cancelT)
29+
30+
setTimeout(() => {
31+
console.log(result) // [{"time":20,"returned":10}]
32+
}, 65)
33+
34+
The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened after the execution of fn(2) at 20ms.
35+
Example 2:
36+
37+
Input: fn = (x) => x**2, args = [2], t = 100, cancelT = 50
38+
Output: []
39+
Explanation: The cancellation was scheduled to occur after a delay of cancelT (50ms), which happened before the execution of fn(2) at 100ms, resulting in fn(2) never being called.
40+
Example 3:
41+
42+
Input: fn = (x1, x2) => x1 * x2, args = [2,4], t = 30, cancelT = 100
43+
Output: [{"time": 30, "returned": 8}]
44+
Explanation: The cancellation was scheduled to occur after a delay of cancelT (100ms), which happened after the execution of fn(2,4) at 30ms.
45+
46+
47+
Constraints:
48+
49+
fn is a function
50+
args is a valid JSON array
51+
1 <= args.length <= 10
52+
20 <= t <= 1000
53+
10 <= cancelT <= 1000
54+
*/
55+
56+
/**
57+
* @param {Function} fn
58+
* @param {Array} args
59+
* @param {number} t
60+
* @return {Function}
61+
*/
62+
const args = [2],
63+
t = 20,
64+
cancelT = 50;
65+
const fn = (x) => x * 5;
66+
const result = [];
67+
68+
// Create a cancellable timeout
69+
var cancellable = function (fn, args, t) {
70+
// Define a cancel function that will clear the timeout
71+
const cancelFn = () => clearTimeout(idTimeout);
72+
73+
// Set up a timeout to execute the function after the specified delay
74+
const idTimeout = setTimeout(() => fn(...args), t);
75+
76+
// When/if we call the function, it will return cancelFn,
77+
// and since the return line calls (and consequentially executes)
78+
// cancelFn, timeout will be cancelled, thereby cancelling fn execution.
79+
return cancelFn;
80+
};
81+
82+
console.log(cancellable(fn, args, t));

Challenges JS/logic/test.html

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
55
<title>Document</title>
66
<script>
7-
var compose = function (functions) {
8-
return function (x) {
9-
return functions.reduceRight((accumulator, currentFunction) => {
10-
return currentFunction(accumulator);
11-
}, x);
12-
};
13-
};
7+
const args = [2],
8+
t = 60,
9+
cancelT = 50;
10+
const fn = (x) => x * 5;
11+
const result = [];
1412

15-
const funcs1 = [(x) => x + 1, (x) => x * x, (x) => 2 * x];
16-
const funcs2 = [(x) => 10 * x, (x) => 10 * x, (x) => 10 * x];
17-
const funcs3 = [];
13+
var cancellable = function (fn, args, t) {
14+
if (t < cancelT) {
15+
return fn(...args);
16+
} else {
17+
//cancel the call of the function
18+
return false;
19+
}
20+
};
1821

19-
console.log(compose(funcs1)(4));
20-
console.log(compose(funcs2)(1));
21-
console.log(compose(funcs3)(42));
22+
console.log(cancellable(fn, args, t));
2223
</script>
2324
</head>
2425
<body></body>

0 commit comments

Comments
 (0)