-1

This is Fibonacci numbers codes in HTML and it is supposed to print out Fibonacci numbers after taking the users input which can be any number

<html>
    <head>
        <script>
          function fib(number) {

    var loop = [0, 1];

    for (var i = 2; i < number; i++) {
        loop[i] = loop[i-1]+ loop[i-2];


    return loop[number-1];
    }

            document.getElementById("output").innerHTML
    }
        </script>

    </head>
    <body>
        <input type text="text" id="txtloop" />
        <input type="button" id="btnEnter" value="Enter" onclick="fib(txtloop.value)" />

        <p id="output"></p>

    </body>
</html>
2
  • 3
    You missed the part that says what your problem is. Commented Nov 14, 2019 at 2:24
  • 1
    document.getElementById("output").innerHTML is there all by itself.... Commented Nov 14, 2019 at 2:25

2 Answers 2

1

Access DOM elements with HTML DOM querySelector

function fib() {
let loop = [0, 1];
let number=document.getElementById('txtloop').value;
for (let i = 2; i < number; i++)
 loop[i] = loop[i-1]+ loop[i-2];
  document.getElementById('output').innerHTML=loop[number-1];
}
<input type text="text" id="txtloop" />
<button onclick="fib()">Enter</button>
<p id="output"></p>

Read more:

Sign up to request clarification or add additional context in comments.

Comments

0

Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . . Each subsequent number is the sum of the previous two.

<html>
<body>

 <input type text="text" id="txtloop" />
        <input type="button" id="btnEnter" value="Enter" onclick="fibonacci_series(txtloop.value)" />

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

<script>
var fibonacci_series = function (n) 
{
  if (n==1) 
  {
var loop = [0, 1];
   document.getElementById("output").innerHTML = loop;
  return loop;
  } 
  else 
  {
    var s = fibonacci_series(n - 1);
    s.push(s[s.length - 1] + s[s.length - 2]);
    document.getElementById("output").innerHTML =s;
    return s;
  }
   
};

</script>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.