Comments (19)
你这个就是数组方法啊,浏览器崩溃是因为循环太多次,导致arr数组太大。解决方法是不要让i循环到num那么大,只需要在arr[i]大于num时跳出循环即可。
from freecodecamp.cn.
查阅大量资料,发现用递归来实现斐波那契数列是有局限性的。
num较小还可以,num较大时,函数调用层数太深,内存会溢出。
解决方法:用数组来缓存每一步的计算结果。
参考资料:http://www.cnblogs.com/meteoric_cry/archive/2010/11/29/1891241.html
from freecodecamp.cn.
我也是做这个算法的时候 写好代码运行然后崩溃了 结果发现是FOR循环里的Num变量导致的 。不过怎么解决啊 ,以后只要一登录这个课程页面就崩溃。。。目前只能换了个浏览器 你有啥办法吗 我的QQ是710107781 你可以联系我
from freecodecamp.cn.
确实如此,我也遇到这种情况。
i的值变大时,不仅浏览器会崩溃,在本地写代码也是如此。
先mark下,解决后再回来更新。
from freecodecamp.cn.
@RUSHtf
把这个页面关闭掉,重新打开链接:https://www.freecodecamp.cn/challenges/sum-all-odd-fibonacci-numbers
注意链接后面不要添加一大串参数,否则会导致重复死循环。
from freecodecamp.cn.
我试了试用 while循环 还是不行。。
from freecodecamp.cn.
@huluoyang
我关闭浏览器 重新打开这个链接:https://www.freecodecamp.cn/challenges/sum-all-odd-fibonacci-numbers 还是会马上崩溃 是不是只要一进去这个页面就会执行一次页面上写的代码?
有没有办法重置一下页面上的代码
from freecodecamp.cn.
@RUSHtf 不会崩溃啊,关闭这个页面即可,然后新开链接。PS.chrome
from freecodecamp.cn.
@huluoyang
太感谢了👏👏👏
from freecodecamp.cn.
from freecodecamp.cn.
我一开始也遇到了这个问题,然后我换了代码就解决了,代码如下:
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.
后来我也是更改了代码通过的,当时是想搞清崩溃的原因是不是因为循环次数,感谢各位解答!
from freecodecamp.cn.
谢谢各位的解答。
from freecodecamp.cn.
应该不是循环次数的原因,而是数组大小的原因,刚刚去FreeCodeCamp的仓库那边看了相关issues,好像 new Array(1000000).join();
也能让页面冻结 。我也写错循环,导致数组太大,然后页面就一直崩溃了,换个浏览器变回初始的代码就能进去了。我觉得清理浏览器对这个网站的缓存应该也是可行的,因为自己写的代码只缓存在本地。
from freecodecamp.cn.
测试后发现确和内存有关,一不小心就崩溃,谢谢各位解答。
from freecodecamp.cn.
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.
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.
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.
用一个两位的数组暂存前两位,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)
- Why I can't pass the test?
- no need to use break HOT 2
- 输入参数检验结果是正确的,但是就是通不过运行
- "25 or More" is wrong ,but I can't fix it. HOT 1
- "9 or Under" is wrong ,but I can't fix it. HOT 1
- why i can't pass Create a Bootstrap Row, pls help?
- Truncate a string 有点问题
- Add your JavaScript Slot Machine Slots 文字错误
- Build a Tribute Page |观看不了右边视频,是否有其他视频源 HOT 1
- 在style标签的最后两段代码,按照要求添加的Id选择器,代码无法通过,并无明显错误,不知道什么原因?
- The Code I write wrong deliberately can run and work as well. HOT 1
- 代码输出正确,却不能通过测试 I'm a little tea pot HOT 1
- 点击校验
- input中type属性checkbook无法识别
- 这个为什么不能过去呢
- Chinese interface:It allows data structures to be any combination of strings, numbers, Booleans, strings, and objects.Did you write "strings" again?
- 这个是BUG吗 HOT 1
- 这样为什么会显示错误
- 为什么过不去?例题和实际无联系。 HOT 1
- 哪里有“我已经完成了这个挑战按钮”啊
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freecodecamp.cn.