Skip to content

Instantly share code, notes, and snippets.

@shigeki
Created November 24, 2011 10:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shigeki/1391018 to your computer and use it in GitHub Desktop.
Save shigeki/1391018 to your computer and use it in GitHub Desktop.
JavaScript で for/while 文を使わないで1から10までの合計を求める方法(node-fibの真似編)
var calc = function(n, callback) {
var sum = function(i, res) {
var func = (n>i) ? sum_tick : function(j,k) {callback(k);};
func(i+1, res+i);
};
var sum_tick = function(i, res) {
process.nextTick(function() { sum(i,res); });
};
sum(1,0);
};
calc(10, function(x) {console.log(x);});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment