# For Loops Understanding

For Loops Understanding
0

#1

hi, I think I have a misunderstanding for the for loop or there is a technical issue. here are 2 pieces of code where fol loop is considered an infinite loop and I don’t know why
1 -

`````` function sumPrimes(num) {

var nums = [2];
var ss =[];
// Not to add all numbers , but add only Primes in the Nums Array by checking if the added number / every number in the Nums array addd before it isn't out of 1 and the number itself
for (var i = 3 ; i <= num ; i++){
for (var j = 0 ; j < nums.length; j++){
var di = i / nums[j];
if (di !== Math.floor(di)){
nums.push(i) ;
}
}
//nums.push(i);
}

var res = nums.reduce(function (a,b){return a+b;},0);
return nums;
}

sumPrimes(20);
``````

2-

``````function sumFibs(num) {
var fiboNums = [1,1];
var firstFibo = 1 ;
var secondFibo = 1 ;
var sum = firstFibo + secondFibo;

fiboNums.push(sum);
for ( var i = 0 ; i < num ; i++){
/* 1 + 1 == 2 ;
1 + 2 == 3
2 + 3 == 5
3 + 5 == 8*/
sum = sum + secondFibo;
fiboNums.push(sum);
}
return fiboNums;
}

sumFibs(4);
``````

#2

So on your first function, what your doing is mutating the condition `nums.length` for the `for()` loop with `nums.push()`. Every time that you push to `nums` it increases the length and `j` won’t trigger the condition.

#3

In `sumPrimes`, the inner for-loop pushes values to the `nums` array. This keeps increasing the value of `nums.length`, and the `j` variable can’t catch up, so you get an infinite loop.

#4

Well, I got what you mean thank you. What about the second one ?! why it is considered as n infinite loop?

#5

That’s right. What about the second piece of code ?!

#6

The second one runs fine (although I presume it gives the wrong answer).

#7

What if I till U that it stops my browser from working !! the second for loop stops the browser until i open this url and change it

Cn u till me what’s wrong ?!

#8

Can you please post your code here. You posted a link to the challenge, but we can not see your code, because it is stored in your local browser.

When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

#9

My code is in the Original Post Content. What I linked was exactly this link : “https://www.freecodecamp.org/challenges/sum-all-odd-fibonacci-numbers?run=disabled

#10

Like I said in the previous post, I can not see the code in your FCC solution. You need to copy/paste it here in the forum as a reply. Please use the 3 backticks on the line before and the line after when you paste it into a post.

#11

Ok that’s it

``````function sumFibs(num) {
var fiboNums = [1,1];
var firstFibo = 1 ;
var secondFibo = 1 ;
var sum = firstFibo + secondFibo;

fiboNums.push(sum);
for ( var i = 0 ; i < num ; i++){
/* 1 + 1 == 2 ;
1 + 2 == 3
2 + 3 == 5
3 + 5 == 8*/
sum = sum + secondFibo;
fiboNums.push(sum);
}
return fiboNums;
}

sumFibs(4);
``````

Is this ok ?

#12

OK. You have a couple of problems with your code. The “freezing” problem is because one of the tests fiboNums(4000000) is taking too long to run, which is causing a problem.

It is taking too long, because your logic is incorrect. You are supposed to return a single number which is the sum of all the odd Fibonacci numbers that are less than or equal to num. If we use the example call of sumFibs(4), your code is returning an array with the following items:

[1, 1, 2, 3, 4, 5, 6]

Why does it return this array?

First, you declare fiboNums and initialize it to have two items, so you start with fiboNums looking like:

[1,1]

Next you have fiboNums.push(sum); which adds the number 2 to the array. At this point, fiboNums looks like:

[1, 1, 2]

Now in the first iteration of your for loop and you have:

``````sum = sum + secondFibo;
fiboNums.push(sum);
``````

The above will make sum = 3 because sum was 2 and secondFibo will ALWAYS be 1 (you never change it in your code). At the end of the first iteration in the for loop, fiboNums looks like:

[1, 1, 2, 3]

The above is repeated 3 more times, because your for loop condition of i < num will have a total of 4 iterations. During each iteration, you are only adding 1 to sum each time. Here is what fiboNum looks like at the end of each of the remaining for loop iterations:

2nd iteration [1, 1, 2, 3, 4]
3rd iteration [1, 1, 2, 3, 4, 5]
4th iteration [1, 1, 2, 3, 4, 5, 6]

Hopefully this helps you understand what is wrong with your current solution.

#13

Great Explanation! I got my wrong in the logic but what i need to make sure that it is right that if the only reason of it considered infinite was it takes too long in 400000 ?!!

#14

There is no need to have an array to solve this challenge. You just need a variable which holds a running sum of ONLY odd Fibonacci numbers. For the number 4000000, there are only 32 Fibonacci numbers less than 4000000 and only 22 of the 32 Fibonacci numbers are odd, so it will run very fast once you figure out the correct algorithm to use.

#15

Actually, I had solved it with for loop Thank You at all for your help and your time