开发者

Javascript One-Liner Debugging

After running the 'ol factorial recursive one-liner through jsperf for the millionth time, I decided to try something a little more interesting ... but it isn't working!

function n(cap){
    return (function y(x){
        return ((x < cap) ? x^2/y(x+1)+2*x-1 : 1)
    }(1))
}

which should work (and return an increasingly precise real value for greater values of 'cap'), however; when run against the numbers 0-19, it produces the following output in Chrome's console:

开发者_开发知识库
1 (x2)
2
0
2 (x16)

I'm at a loss. When stepped through, given the call stack, it's obviously recursing but fails to return anything other than natural numbers. Any thoughts?


it's your ^, which is a bitwise xor, not a power operator. To raise something: Math.pow(2, 10) == 1024

All bitwise ops in JavaScript have an implicit cast-to-int, meaning 0^3.14159265358979323846 == 3

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜