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 bitwisexor, not a power operator. To raise something:Math.pow(2, 10) == 1024All bitwise ops in JavaScript have an implicit cast-to-int, meaning
0^3.14159265358979323846 == 3