GithubHelp home page GithubHelp logo

Comments (19)

zsqosos avatar zsqosos commented on May 1, 2024 9

你这个就是数组方法啊,浏览器崩溃是因为循环太多次,导致arr数组太大。解决方法是不要让i循环到num那么大,只需要在arr[i]大于num时跳出循环即可。

from freecodecamp.cn.

huluoyang avatar huluoyang commented on May 1, 2024 7

查阅大量资料,发现用递归来实现斐波那契数列是有局限性的。
num较小还可以,num较大时,函数调用层数太深,内存会溢出。
解决方法:用数组来缓存每一步的计算结果。
参考资料:http://www.cnblogs.com/meteoric_cry/archive/2010/11/29/1891241.html

from freecodecamp.cn.

RUSHtf avatar RUSHtf commented on May 1, 2024

我也是做这个算法的时候 写好代码运行然后崩溃了 结果发现是FOR循环里的Num变量导致的 。不过怎么解决啊 ,以后只要一登录这个课程页面就崩溃。。。目前只能换了个浏览器 你有啥办法吗 我的QQ是710107781 你可以联系我

from freecodecamp.cn.

huluoyang avatar huluoyang commented on May 1, 2024

确实如此,我也遇到这种情况。
i的值变大时,不仅浏览器会崩溃,在本地写代码也是如此。
先mark下,解决后再回来更新。

from freecodecamp.cn.

huluoyang avatar huluoyang commented on May 1, 2024

@RUSHtf
把这个页面关闭掉,重新打开链接:https://www.freecodecamp.cn/challenges/sum-all-odd-fibonacci-numbers
注意链接后面不要添加一大串参数,否则会导致重复死循环。

from freecodecamp.cn.

RUSHtf avatar RUSHtf commented on May 1, 2024

我试了试用 while循环 还是不行。。

from freecodecamp.cn.

RUSHtf avatar RUSHtf commented on May 1, 2024

@huluoyang
我关闭浏览器 重新打开这个链接:https://www.freecodecamp.cn/challenges/sum-all-odd-fibonacci-numbers 还是会马上崩溃 是不是只要一进去这个页面就会执行一次页面上写的代码?
有没有办法重置一下页面上的代码

from freecodecamp.cn.

huluoyang avatar huluoyang commented on May 1, 2024

@RUSHtf 不会崩溃啊,关闭这个页面即可,然后新开链接。PS.chrome

from freecodecamp.cn.

RUSHtf avatar RUSHtf commented on May 1, 2024

@huluoyang
太感谢了👏👏👏

from freecodecamp.cn.

jamesbeme avatar jamesbeme commented on May 1, 2024

from freecodecamp.cn.

cllgeek avatar cllgeek commented on May 1, 2024

我一开始也遇到了这个问题,然后我换了代码就解决了,代码如下:

var sumFibs = function() {
    var cache = [1, 1];
    return function (n) {
        if (n >= cache.length) {
            for (var i = cache.length; i < n ; i++ ) {
                cache[i] = cache[i - 2] + cache[i - 1];
            }
        }
        var arr=cache.filter(function(val){
           return val%2!=0&&val<=n;
         });
        return arr.reduce(function(pre,next){
          return pre+next;
        });
    };

}();

sumFibs(400);

from freecodecamp.cn.

craiiiigie avatar craiiiigie commented on May 1, 2024

后来我也是更改了代码通过的,当时是想搞清崩溃的原因是不是因为循环次数,感谢各位解答!

from freecodecamp.cn.

KKenny0 avatar KKenny0 commented on May 1, 2024

谢谢各位的解答。

from freecodecamp.cn.

Andy201512 avatar Andy201512 commented on May 1, 2024

应该不是循环次数的原因,而是数组大小的原因,刚刚去FreeCodeCamp的仓库那边看了相关issues,好像 new Array(1000000).join(); 也能让页面冻结 。我也写错循环,导致数组太大,然后页面就一直崩溃了,换个浏览器变回初始的代码就能进去了。我觉得清理浏览器对这个网站的缓存应该也是可行的,因为自己写的代码只缓存在本地。

from freecodecamp.cn.

SnoreCrocodile avatar SnoreCrocodile commented on May 1, 2024

测试后发现确和内存有关,一不小心就崩溃,谢谢各位解答。

from freecodecamp.cn.

prc333 avatar prc333 commented on May 1, 2024

function sumFibs(num) {
var a1=1,a2=1;
var sum=2;
for(var i=0;a2<=num;i++){
var a =a1;
a1=a2;
a2+=a;
if(a2%2 === 1&&a2<=num){
sum+=a2;

}

}

return sum;
}

sumFibs(4);
可以试试这个

from freecodecamp.cn.

C-C-Y avatar C-C-Y commented on May 1, 2024

function sumFibs(n) {
var x = [1,1];
var y = 2;
for(i=2;x[i-1]<=n;i++){
x[i]=x[i-2]+x[i-1];
if(x[i]%2==1&&x[i]<=n){
y+=x[i];
}
}
return y;
}

sumFibs(75025);

from freecodecamp.cn.

NidusP avatar NidusP commented on May 1, 2024

function sumFibs(num) {
var arr = [1,1];
var s = 2;
while(arr[arr.length-1]+arr[arr.length-2]<=num){
arr[arr.length]= arr[arr.length-1]+arr[arr.length-2];
console.log(arr[arr.length]);
if(arr[arr.length-1]%2==1){ s+=arr[arr.length-1];}
}
return s;
}

from freecodecamp.cn.

Cuphoria avatar Cuphoria commented on May 1, 2024

用一个两位的数组暂存前两位,result存贮目前的位,但值大于给定的num即推出,否则把奇数加到sum
function sumFibs(num) {
if(num<1) return 0;
var arr=[1,1];
var result=0,sum=2;
for(var i=2;result<=num;++i)
{
result=arr[0]+arr[1];
if(result>num) break;
if(result%2!=0) sum+=result;
if(i%2==0) arr[0]=result;
else arr[1]=result;

}
return sum;

}

sumFibs(4);

from freecodecamp.cn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.