-4

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 />");
}

Best, I'm doing a Fibonacci Series, but I want to see a message with prompt comes up, so you can fill in a number, then the returns with the Fibonacci Series. Who would be able to help me? Above I have now.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Gigi
  • 9
  • 1
  • 1
  • 2

10 Answers10

3

I have created demo, hope it will help you.

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

Vikram Rajput
  • 101
  • 1
  • 9
2

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>
2

Using ES6 generator

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]
0

I hope that this is what you are looking for.

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.

chris97ong
  • 6,870
  • 7
  • 32
  • 52
  • not exactly. I can fill in a code prompt with you and I want that too. but it also comes in a prompt to see replies when you code and I want to see the code in an input (html). I dont want to see the answer in the prompt, but in the html code. Thank you – Gigi Apr 07 '14 at 10:25
  • @Gigi: in your question please take the time to clarify precisely what you want, as it stands you're getting partial or incorrect answers because you've not explained yourself or your requirements. What should the user do? What, precisely, should happen in response? – David Thomas Apr 07 '14 at 10:29
  • Sorry for my bad English. The translate was very bad. No i have a question ban..... – Gigi Apr 10 '14 at 10:12
0

you need to use a prompt box to get the number:

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

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

Aidan
  • 1,550
  • 1
  • 13
  • 20
0
var number = prompt("Enter number ");

....... your code ....

Tomtom
  • 9,087
  • 7
  • 52
  • 95
0

You can do the following:

    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);
Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
Aashray
  • 2,753
  • 16
  • 22
0
<!doctype html>
<html lang="en">
 <head>
 <body>

 <script type="text/javascript">
 var f1=0,f2=1,f3;
 var i;
alert("enter a text"+n);
 var n=prompt("enter the number");
 document.write("the fibonacci series is "+"<br/>");

   for(i=2;i<=n;i++)
   {

       f3=f1+f2;
       f1=f2;
       f2=f3;
     document.write(f3+"<br/>");   
   }
   </script>
   <style>

  body {background-color:#66ff66}
 </style>

 </head>
 </body>
vijay_ky
  • 41
  • 6
0

i came up with this solution to get the n index Fibonacci value. you can use the findFac0(); to pass the index you need to get the Fibonacci value.

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));
Viraj
  • 638
  • 1
  • 8
  • 10
0

I've tried this: it might work:

<!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>
Green
  • 486
  • 2
  • 11
  • 31