For some frustrating reason,
CMD
suddenly stopped working after I completed the third-to-last problem. I couldn’t select any more problems, and this issue occurred on both my work and personal computers. So, I ended up giving up on the last three problems and didn’t have the energy to figure out what they were. I’ll just leave it at that.
During the problem-solving process, there was one important point to note, which involves a very common method in JavaScript. That is, functions delayed by setTimeout
will only execute after the preceding functions have completely finished. In other words, it can block the JavaScript process. For example:
function repeat(operation, num) {
// modify this so it can be interrupted
if (num <= 0) return;
operation();
setTimeout(function () {
return repeat(operation, --num);
}, 0);
}
module.exports = repeat;
Operation
is a time-consuming function. If the subsequent recursive calls are not delayed using setTimeout
, it would result in a situation where the repeat
function has already finished executing, but many operation
calls within it have not yet completed.
Other than that, there’s nothing much to add. All the code is available here.
This article was published on September 24, 2015 and last updated on September 24, 2015, 3665 days ago. The content may be outdated.