我正在使用Codewars問題'Happy Numbers'這是鏈接https://www.codewars.com/kata/happy-numbers-5/train/javascript這是問題,當我在n> 98時運行代碼時已達到最大調用堆棧大小。如何對我的代碼進行一些更改以解決此問題?function happyNumbers(x){ var res = []; for (let i = 1; i <= x; i++){ var str = []; if (helper(str,i)){res.push(i)} } return res}function helper(str,n){ var num = 0; if (n === 1){return true} if (str.indexOf(n) > -1){return false} str.push(n); if (n.toString().length === 1){num = Math.pow(n,2).toString()} if (n.toString().length >= 2){ num = n.toString().split('') .reduce((a,b) => Math.pow(a,2)+ Math.pow(b,2)).toString(); } return helper(str,Number(num))}
Codewars問題“快樂數字”我如何才能對我的代碼進行一些更改以使其正常工作?
一只斗牛犬
2021-05-03 12:35:01