fibonacci series in javascript in range using document getelementbyid

Solutions on MaxInterview for fibonacci series in javascript in range using document getelementbyid by the best coders in the world

showing results for - "fibonacci series in javascript in range using document getelementbyid"
Amaury
09 Oct 2020
1
2<!DOCTYPE html>
3<html>
4
5<head>
6    <title>Fibonacci Series</title>
7</head>
8<script>
9    function generateFib() {
10        var range = document.getElementById("range").value;
11
12        document.getElementById("fibHead").innerHTML = 'Fibonacci Series';
13        if (range <= 0) {
14            document.getElementById("fibSeries").innerHTML = 'Cannot print fibonacci series';
15        }
16        var term_one = 0;
17        var term_two = 1;
18        var fib;
19
20        if (range == 1)
21            document.getElementById("fibSeries").innerHTML = term_one;
22        else if (range >= 2) {
23            document.getElementById("fibSeries").innerHTML = term_one + '<br>' + term_two;
24            for (var i = 0; i < range - 2; i++) {
25                fib = term_one + term_two;
26                document.getElementById("fibSeries").innerHTML +=  '<br>' + fib;
27                term_one = term_two;
28                term_two = fib;
29            }
30        }
31    }
32</script>
33
34<body>
35
36    <input type="number" id="range" name="range">
37    <button type="submit" onclick="generateFib()">Generate</button>
38    <p id="fibHead"></p>
39    <p id="fibSeries"></p>
40</body>
41
42</html>
43
44