Javascript looping through Fibonacci numbers and testing for even numbers

假装没事ソ 提交于 2019-12-18 09:55:09

问题


I am trying to “displays the sum of first 50 even Fibonacci numbers” and currently am getting the wrong output.

I need to:

  1. create a loop that generates Fibonacci numbers.

  2. test each one for whether it's even or odd.

  3. Add up up the even ones, counting them as you go.

I am new to JavaScript and am having trouble getting my code to work. Any help/guidance is greatly appreciated. Is their a better, simpler way?

------------HERE IS MY CODE THUS FAR --------

<div id="sumFib" class="hwbutton">Get the Sum!</div><br>

The sum of the first 50 even Fibonacci numbers is: <span class="" id="sumFibResult"></span>
<br>

<script type="text/javascript">
var getFibSum = document.getElementById("sumFib");
getFibSum.onclick = function(){
fiftyEvenFibonacciSum();
 }

function fiftyEvenFibonacciSum(){

var loopFib;

//Initialize fibonacci array

var fibonacci = new Array();

//Add fibonacci array items
fibonacci[0] = 0;
fibonacci[1] = 1;
var sum=0;

//Since it takes 150 fib numbers to obtain 50 even, loop through that many.
for(loopFib=2; loopFib<=150; loopFib++) {

    // Next fibonacci number = previous + one before previous
    fibonacci[loopFib] = fibonacci[loopFib-2] + fibonacci[loopFib-1];

    //test for even numbers with if then statement
    var integer = parseInt(fibonacci[loopFib]);

    if (integer % 2 == 0) {

    //Add up the even fib numbers if even and output into dispay variable
    var display = sum += fibonacci[loopFib];

    //output results to html page
    document.getElementById("sumFibResult").innerHTML = display ;

}
}
}
</script>

回答1:


Try This i have added counter to stop the execution of adding even number after it reaches 50, let me if it works

    var fibonacci = new Array();

//Add fibonacci array items
fibonacci[0] = 0;
fibonacci[1] = 1;
var sum=0;

//Since it takes 150 fib numbers to obtain 50 even, loop through that many.
var counter=0;
for(loopFib=2; loopFib<=150; loopFib++) {

    // Next fibonacci number = previous + one before previous
    fibonacci[loopFib] = fibonacci[loopFib-2] + fibonacci[loopFib-1];

    //test for even numbers with if then statement
    var integer = parseInt(fibonacci[loopFib]);

    if (integer % 2 == 0  && counter < 50) {
        counter++;
        //Add up the even fib numbers if even and output into dispay variable
        var display = sum += fibonacci[loopFib];
    //console.log(fibonacci[loopFib]);   //output results to html page
            //console.log(counter);  
        document.getElementById("sumFibResult").innerHTML = display ;

}
}



回答2:


Here's how I did it.

let num = 1;
let sum = 0;
let counter = 0;

function evenCheck(x)
{
    if(x%2===0)
    {
        return true;
    }
return false;
}

for(let i=0; i<1000; i+=num)
{
    num += i;
    if(counter>=50)
    {
        break;
    }
    if(evenCheck(i))
    {
        sum += i;
        counter++;
    }
    if(evenCheck(num))
    {
        sum += num;
        counter++;
    }
}
console.log(sum);


来源:https://stackoverflow.com/questions/22240354/javascript-looping-through-fibonacci-numbers-and-testing-for-even-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!