admin 管理员组文章数量: 1086019
I am trying to solve a problem of leetcode through recursion but I am getting an error saying RangeError: Maximum call stack size exceeded
maybe I am doing something wrong.
Problem:
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy.
Return true if n
is a happy number, and false
if not.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
My Code:
var isHappy = function(n) {
if(n.length<2) return false
var fn=(n)=>{
let i=0;
let sum=0;
while(i<n.length){
sum=sum+Math.pow(n[n.length-1-i],2);
i++;
}
while(sum!==1){
//console.log(sum)
fn(sum);
}
if(sum === 1) return true
}
fn(n)
};
PS: I don't need a solution for this problem. I want to find out why my code isn't working and What I am doing wrong. And, What changes should I make so that it works fine.
Link to the above problem.
I am trying to solve a problem of leetcode through recursion but I am getting an error saying RangeError: Maximum call stack size exceeded
maybe I am doing something wrong.
Problem:
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy.
Return true if n
is a happy number, and false
if not.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
My Code:
var isHappy = function(n) {
if(n.length<2) return false
var fn=(n)=>{
let i=0;
let sum=0;
while(i<n.length){
sum=sum+Math.pow(n[n.length-1-i],2);
i++;
}
while(sum!==1){
//console.log(sum)
fn(sum);
}
if(sum === 1) return true
}
fn(n)
};
PS: I don't need a solution for this problem. I want to find out why my code isn't working and What I am doing wrong. And, What changes should I make so that it works fine.
Link to the above problem.
Share Improve this question edited Apr 15, 2022 at 13:50 Harsh Mishra asked Apr 15, 2022 at 13:19 Harsh MishraHarsh Mishra 9784 gold badges17 silver badges38 bronze badges 2-
1
In your
while
loop you check the value ofsum
but never actually change it anywhere! – Joachim Sauer Commented Apr 15, 2022 at 13:36 - @JoachimSauer What changes should I make in my code then? – Harsh Mishra Commented Apr 15, 2022 at 13:59
2 Answers
Reset to default 1As you wrote yourself, "Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle".
In your implementation, fn
calls itself infinitely. In the process, the javascript virtual machine creates a fonction context in memory at each iteration. This can be done up to a certain times after which the error Maximum call stack size exceeded
throws.
What I am doing wrong?
You return true when you find a happy number, but you never return false otherwise. Add a condition to detect unhappy numbers (detect a cycle) and return false in that case.
Edit: here is an example implementation:
var isHappy = function(n) {
if(n.length<2) return false
// placeholder to store already called values
const calledValues= new Set()
var fn=(n)=>{
let i=0;
let sum=0;
if (calledValues.has(n))
// cycle detected!
return false;
calledValues.add(n);
while(i<n.length){
sum=sum+Math.pow(n[n.length-1-i],2);
i++;
}
if (sum !== 1)
return fn(sum.toString());
else
// sum === 1, number is happy!
return true
}
// make sure to pass a string to fn
return fn(n.toString());
};
(new Array(20)).fill().map((_,i)=>10+i)
.forEach(n=> console.log(n, "is happy?", isHappy(n)));
Maximum call stack size exceeded
This error occurs when functions call other functions too many times. In this case, your function calls itself recursively with no end, should the number be an "unhappy" one.
Consider adding into your function something which allows you to tell if the process loops endlessly.
Consider using arguments/arrays to store what numbers have already turned up.
本文标签: recursionI am getting RangeError Maximum call stack size exceeded error in JavascriptStack Overflow
版权声明:本文标题:recursion - I am getting RangeError: Maximum call stack size exceeded error in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744059127a2526396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论