Skip to content

Commit

Permalink
solved #13: setTimeout, call f(x) every second for n seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Sophia committed Oct 28, 2017
1 parent 919d95a commit 68dba6d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions solutions/13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const customTimeout = (fun, n) => {
if (n > 0) {
setTimeout(() => {
fun();
customTimeout(fun, n-1);
}, 1000);
}
};
module.exports = customTimeout;
14 changes: 14 additions & 0 deletions test/13.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const customTimeout = require ('../solutions/13.js');
const test = (testNumber) => {
let i=0;
const countFun = () => {
i++;
if (i === testNumber) {
console.log(`Correct! Function repeated ${testNumber} times every second.`);
} else {
console.log(`Function repeated ${i} times.`);
}
};
customTimeout(countFun, testNumber);
};
test(5);

0 comments on commit 68dba6d

Please sign in to comment.