Skip to content

Commit 407976c

Browse files
committed
added problems
1 parent 70e5b44 commit 407976c

File tree

10 files changed

+129
-0
lines changed

10 files changed

+129
-0
lines changed

csbin/async/01-sayHowdy.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function sayHowdy() {
2+
console.log('Howdy');
3+
}
4+
5+
function testMe() {
6+
setTimeout(sayHowdy, 0);
7+
console.log('Partnah');
8+
}
9+
10+
// 'Partnah'
11+
// 'Howdy'
12+
// After thinking it through, uncomment the following line to check your guess!
13+
testMe(); // what order should these log out? Howdy or Partnah first?

csbin/async/02-delayedGreet.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function delayedGreet() {
2+
setTimeout(() => {
3+
console.log("welcome")
4+
}, 3000);
5+
}
6+
delayedGreet(); // should log (after 3 seconds): welcome

csbin/async/03-helloGoodbye.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function helloGoodbye() {
2+
console.log("hello");
3+
setTimeout(() => {
4+
console.log("good bye")
5+
}, 3000);
6+
}
7+
helloGoodbye(); // should log: hello // should also log (after 3 seconds): good bye

csbin/async/04-brokenRecord.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function brokenRecord() {
2+
setInterval(() => {
3+
console.log("hi again")
4+
}, 1000);
5+
}
6+
7+
brokenRecord(); // should log (every second): hi again

csbin/async/05-limitedRepeat.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function limitedRepeat() {
2+
const interval = setInterval(() => {
3+
console.log("hi for now");
4+
}, 1000);
5+
6+
setTimeout(() => clearInterval(interval), 5100);
7+
}
8+
9+
limitedRepeat(); // should log (every second, for 5 seconds): hi for now
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function everyXsecsForYsecs(func, interval, duration) {
2+
const timer = setInterval(() => func(), interval);
3+
setTimeout(() => clearInterval(timer), duration);
4+
}
5+
// Uncomment the following lines to check your work!
6+
function theEnd() {
7+
console.log('This is the end!');
8+
}
9+
// everyXsecsForYsecs(theEnd, 2, 20); // should invoke theEnd function every 2 seconds, for 20 seconds): This is the end!
10+
everyXsecsForYsecs(theEnd, 1000, 5000);

csbin/async/07-delayCounter.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function delayCounter(target, wait) {
2+
let num = 1;
3+
return function() {
4+
const timer = setInterval(() => {
5+
console.log(num);
6+
if (num === target) {
7+
clearInterval(timer);
8+
return;
9+
}
10+
num++;
11+
}, wait)
12+
}
13+
}
14+
15+
const countLogger = delayCounter(3, 1000)
16+
countLogger();
17+
// After 1 second, log 1
18+
// After 2 seconds, log 2
19+
// After 3 seconds, log 3

csbin/async/08-promised.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function promised (val) {
2+
return new Promise(resolve => {
3+
setTimeout(() => {
4+
resolve(val)
5+
}, 2000);
6+
})
7+
}
8+
9+
const createPromise = promised('wait for it...');
10+
createPromise.then((val) => console.log(val));
11+
// will log "wait for it..." to the console after 2 seconds

csbin/async/09-SecondClock.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class SecondClock {
2+
constructor(cb) {
3+
this.cb = cb;
4+
}
5+
6+
start() {
7+
let seconds = 1;
8+
this.timer = setInterval(() => {
9+
this.cb(seconds);
10+
seconds++;
11+
}, 1000);
12+
}
13+
14+
reset() {
15+
clearInterval(this.timer);
16+
}
17+
}
18+
19+
const clock = new SecondClock((val) => { console.log(val) });
20+
console.log("Started Clock.");
21+
clock.start();
22+
setTimeout(() => {
23+
clock.reset();
24+
console.log("Stopped Clock after 6 seconds.");
25+
}, 6000);

csbin/async/10-debounce.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function debounce(callback, interval) {
2+
let timer;
3+
let isIntervalElapsed = true;
4+
return function() {
5+
if (isIntervalElapsed) {
6+
isIntervalElapsed = false;
7+
return callback();
8+
}
9+
10+
clearTimeout(timer);
11+
timer = setTimeout(() => {
12+
isIntervalElapsed = true;
13+
}, interval);
14+
}
15+
}
16+
17+
function giveHi() { return 'hi'; }
18+
const giveHiSometimes = debounce(giveHi, 3000);
19+
console.log(giveHiSometimes()); // -> 'hi'
20+
setTimeout(function() { console.log(giveHiSometimes()); }, 2000); // -> undefined
21+
setTimeout(function() { console.log(giveHiSometimes()); }, 4000); // -> undefined
22+
setTimeout(function() { console.log(giveHiSometimes()); }, 8000); // -> 'hi'

0 commit comments

Comments
 (0)