It is necessary to write a function that generates sequentially the numbers of the Fibonacci series.

At the user's request, the element numbers using the written function display the elements of the Fibonacci series until the user stops requesting them

It turned out to implement this task, but the series becomes visible only after the end of the requests:

var fibonacci = [0, 1]; do { var n = +prompt("Введите значение",""); Fib(n); } while (n>0); function Fib() { for (i = 2; i < n; i ++) { fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; } document.write(fibonacci.slice(n-1,n) + '<br>'); } 

    1 answer 1

    The code does not have time to appear because the cycle is more priority than changing the document. I redid your code to call the next prompt only after 50 ms (for the document to change), for this I had to redo the loop into the recursion function. It is also better not to use document.write , at least in real projects, I replaced it with innerHTML .

     var fibonacci = [0, 1]; ask() function ask () { var n = +prompt("Введите значение",""); if (!n) return Fib(n); setTimeout(ask, 50) } function Fib(n) { for (i = 2; i < n; i ++) { fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; } document.body.innerHTML += fibonacci.slice(n-1,n) + '<br>'; }