Thursday 15 July 2010

javascript - Click button to get the sum of the first 12 Fibonacci numbers -


I want to get the Fibonacci number of 12 too and when I click on the button, it appears on my page Is just getting my code and even adding numbers too. How can I limit it to the first 12 numbers?

Thanks in advance

  // First we get the HTML for the button getFibSum = document.getElementById ("sumFib"); // When we click the button getFibSum.onclick = function () {click.getElementById ("sumFibResult"), we set the event handler. InnerHTML = twelveEvenFibonacciSum (); } / * * Twelvefenbicacinum - The number of first 12 also collects Fibonacci numbers, with 0, the first two numbers of the first sequence of the sequence / function of twelve vanefibonaxis () {/// your code here // loop which generates Fibonacci numbers is . Console.clear (); Var fib = [0, 1]; // start array var sum = 0; For (var i = 2; i & lt; = 50; i ++) {fib [i] = fib [i-1] + fib [i-2]; Var integer = fib [i]; If (integer% 2 == 0) {// console.log ("My current is also fib" + fib [i]); Var sumFibResult = sum + = fib [i]; Console.log ("My current addition was added to Yoga to sum up" + sumFibResult); } // loop as long as we do not have 12 numbers // // consolesole.log (fib); Return sumFibResult; }  

Add a variable to the outer scope that increases your function every time. After return it is equal to 12.

 ; Var integer = fib [i]; If (integer% 2 == 0) {// console.log ("My current is also fib" + fib [i]); Var sumFibResult = sum + = fib [i]; Console.log ("My current addition was added to Yoga to sum up" + sumFibResult); TimesIncremented ++; } // loop as long as we do not have 12 numbers, if (bar is notched> = 12) {return; }}  

No comments:

Post a Comment