The while loop runs whilst the current number is less than or equal to the number specified, so it will run until you reach the argument passed to the function.
As we only want to sum all odd numbers, the if statement determines whether the modulus (remainder) of the current number divided by 2 is 0 (identifying it as an even number). If the remainder is not 0, the number is odd and is added to the result:
The application then moves onto the next number to test to continue the loop until it has checked all values:
Please don’t post the code from the guide, we are trying to reduce the number of full-working solutions. If you hahve questions about a guide solution please post the link to the guide and explain which of the solutions you are talking about and what is your question
the guide does not have any other working code so here is another simple hair splitter that uses for loop hope this will help better visualization of the Fibonacci process:
function sumFibs(num) {
var i,ii,iii=2,y=1;
for (i=0;i<=num; ){
i<3 ? ii=1 : ii=iii;
i%2===0? "" : y=y+i ;
iii=i; //forward reference to be used by ii
i=i+ii; //incrementing i to loop
}
return y;
}
sumFibs(10);
==================
the solution had to be visualized using this mockup pattern:
where 1, 1, 2, 3, 5 and 8... as trail pattern
i, ii = iv
1 (first number in fibonacci)
iii 0, 1 = 1 (second number which is sum of iterator i and ii)
iii 1, 1 = 2
iii 2, 1 = 3 (ii is 1 until i is >=3)
iii 3, 2 = 5
iii 5, 3 = 8
@ILM sorry mods for sharing code … I hope that this may shed some light…