2016-07-27 23 views
-3

我試圖做的,方便日常程序員在Reddit上的node.js中鏈接:https://www.reddit.com/r/dailyprogrammer/comments/4uhqdb/20160725_challenge_277_easy_simplifying_fractions/我的程序,以簡化分數被打調用堆棧限制。爲什麼?

我寫了一個快速程序,但它給輸出有點不同,上破高的。

我的代碼:

const readline = require("readline"); 
const rl = readline.createInterface({ 
    input: process.stdin, 
    output: process.stdout 
}); 

function gcd(a, b) { 
    if (b == 0) { 
    return a; 
    } 

    return gcd(b, a & b); 
} 

function all() { 
    rl.question("wewe? ", (answer) => { 
    if (answer === "x") { 
     rl.close(); 
     return; 
    } 
    var str = answer.split(" "); 
    var num = +str[0]; 
    var den = +str[1]; 
    var div = gcd(num, den); 
    console.log(num/div + " " + den/div); 
    all(); 
    }); 
} 

all(); 

這是電流輸出:

[email protected] /development/randomcode/dailyprogramming$ node Simplifying\ fractions.js 
wewe? 4 8 
0.5 1 
wewe? 1536 78360 
readline.js:924 
      throw err; 
      ^

RangeError: Maximum call stack size exceeded 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:7:13) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 
    at gcd (/development/randomcode/dailyprogramming/Simplifying fractions.js:12:10) 

預期輸出:

1 2 
64 3265 
+2

你就會知道爲什麼,如果你把一個'console.log'打電話給你的'gcd'函數中。 – nugae

+0

它似乎停留在512×512 – Snorflake

回答

0

這是一個簡單的輸入錯誤!該死的我的新鍵盤。我無法相信我做到了。

我用4 & 8時,它應該是4%,8

+0

它發生在我們身上都得到! – nugae

+1

它的凌晨1點! :(不要責怪我:頁 – Snorflake

相關問題