简体   繁体   English

Javascript 单线调试

[英]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!在第一百万次通过 jsperf 运行 'ol 阶乘递归单行之后,我决定尝试一些更有趣的东西......但它不起作用!

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:当针对数字 0-19 运行时,它会在 Chrome 的控制台中生成以下 output:

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.这是您的^ ,它是按位xor而不是幂运算符。 To raise something: Math.pow(2, 10) == 1024提出一些问题: Math.pow(2, 10) == 1024

All bitwise ops in JavaScript have an implicit cast-to-int, meaning 0^3.14159265358979323846 == 3 JavaScript 中的所有按位运算都具有隐式转换为整数,即0^3.14159265358979323846 == 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM