斐波纳契与Javascript

时间:2014-04-07 10:16:43

标签: javascript html fibonacci

 <script>
    var var1 = 0;
    var var2 = 1;
    var var3;

    var num = 20;

    document.write(var1+"<br />");
    document.write(var2+"<br />");

    for(var i=3; i <= num;i++)
    {
        var3 = var1 + var2;
        var1 = var2;
        var2 = var3;

        document.write(var3+"<br />");
    }
</script>

最好,我正在做一个斐波那契系列,但我想看到一条带有提示的消息,所以你可以填写一个数字,然后用斐波纳契系列返回。谁能帮助我?我现在已经上面了。

由于

10 个答案:

答案 0 :(得分:2)

我已经创建了演示,希望它会对您有所帮助。

var i;
var fib = [0, 1];
var limit = window.prompt('Enter the limit for your series:', '');
for (i = 2; i < parseInt(limit); i++) {
    fib[i] = fib[i - 2] + fib[i - 1];
}
console.log(fib);

Demo Link

答案 1 :(得分:1)

使用ES6生成器

function* fiboGen(len, current = 0, next = 1) {
      if (len === 0) {
        return current;
      }
      yield current;
      yield* fiboGen(len - 1, next, current + next);
    }
    
    const fibo = [...fiboGen(10)];
    
    console.log(fibo);
//[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

答案 2 :(得分:1)

我已经尝试过:可能有效:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fibonacci</title>
    <style>
        * {
            outline: 0px;
            margin: 0px;
        }

        input[type="number"] {
            color: blue;
            border: 2px solid black;
            width: 99.58vw;
        }
    </style>
</head>

<body>
    <div id="myDiv" style="color: white;background-color: blue;">Numbers Here</div>
    <input type="number" id="input1" oninput="fibonacciProgram(this.value)" placeholder="Type Some Numbers Here">
    <script>
        function fibonacciProgram(numberCount) {
            let resultElement = document.getElementById("myDiv");
            resultElement.innerHTML = " ";
            if (isNaN(numberCount) || numberCount <= 0) {
                resultElement.innerHTML = "please enter a number";
                return;
            }
            let firstBox = 0;
            let secondBox = 1;
            let swichBox;
            let entries = [];
            entries.push(secondBox);
            while (numberCount > 1) {
                swichBox = firstBox + secondBox;
                entries.push(swichBox);
                firstBox = secondBox;
                secondBox = swichBox;
                numberCount--;
            }
            resultElement.innerHTML = entries.join(', ');
        }
    </script>
</body>

</html>

FIBONACCI Here's something

答案 3 :(得分:0)

我希望这就是你要找的东西。

<强> HTML

Which sequence of the Fibonacci pattern do you want to find?
<br><br>
<input type="text" id="inputtext">
<br><br>
<input type="button" value="Find out" id="btn">
<br><br>
<b id="ID"></b>

<强> JS

    function add(a, b) {
    while (a.length < b.length) a.unshift(0);
    while (a.length > b.length) b.unshift(0);
    var carry = 0,
        sum = [];
    for (var i = a.length - 1; i >= 0; i--) {
        var s = a[i] + b[i] + carry;
        if (s >= 10) {
            s = s - 10;
            carry = 1;
        } else {
            carry = 0;
        }
        sum.unshift(s);
    }
    if (carry) sum.unshift(carry);
    return sum;
}

function fib(n) {
    var f1 = [0];
    var f2 = [1];

    while (n--) {
        var f3 = add(f1, f2);
        f1 = f2;
        f2 = f3;
    }
    return f1.join("");
}

document.getElementById("btn").onclick = function () {
        var inputnum = parseFloat(document.getElementById("inputtext").value);
        document.getElementById("ID").innerHTML = fib(inputnum).toString();
};

Fiddle

答案 4 :(得分:0)

您需要使用提示框来获取数字:

window.prompt("sometext","defaultText");

http://www.w3schools.com/js/js_popup.asp

答案 5 :(得分:0)

var number = prompt("Enter number ");

.......你的代码......

答案 6 :(得分:0)

您可以执行以下操作:

    var var1 = 0;
    var var2 = 1;
    var var3;
    var num = window.prompt("Enter the limit for your series:","");
    //var num = 20;
    var str = '';
    str+=var1+','+var2;

    for(var i=3; i <= parseInt(num);i++)
    {
        var3 = var1 + var2;
        var1 = var2;
        var2 = var3;
        str+=','+var3;
    }
    document.write(str);

答案 7 :(得分:0)

foreach(var Book in BooksList)
{
    Console.WriteLine(Book.BookCategory);
    //logic to Obtain Book Names Here ;
}

答案 8 :(得分:0)

function myFunction() {
    var n = document.getElementById("myNumber").value;
    document.getElementById("demo").innerHTML = fibonacciGenerator (n);
}

var sequence = [0]; // sequence = []; if you want sequence [1] = 1;
var previousNumber = 1;
var presentNumber = 0;
var sum = 0;
 
function fibonacciGenerator (n) {
  
    while (sequence.length < n) {
     sum = previousNumber + presentNumber;
     previousNumber = presentNumber;
     presentNumber = sum;
     sequence.push(sum);
     
    }
    return (sequence);
}
<!DOCTYPE html>
<html>
<body>

<h1>Fibonacci Generator </h1>

<input type="number" id="myNumber" value="0">

<p>Enter a number to generate Fibonacci array.</p>

<button onclick="myFunction()">Try it</button>


<p id="demo"></p>


</body>
</html>

答案 9 :(得分:0)

我提出了这个解决方案来获得n指数斐波那契值。您可以使用findFac0();传递获得斐波那契值所需的索引。

function findFac(n){
if (n===1) 
  {
   return [0, 1];
  } 
  else 
  {
    var s = findFac(n - 1);
    s.push(s[s.length - 1] + s[s.length - 2]);
    return s;
  }
}

function findFac0(n){
var vv1 = findFac(n);
return vv1[n-1];
}


console.log(findFac0(10));