Created
November 24, 2011 10:05
-
-
Save shigeki/1391018 to your computer and use it in GitHub Desktop.
JavaScript で for/while 文を使わないで1から10までの合計を求める方法(node-fibの真似編)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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