DEV Community

Cover image for Daily code 56 | Fibonacci Sequence Generator
Gregor Schafroth
Gregor Schafroth

Posted on

Daily code 56 | Fibonacci Sequence Generator

Alright time for an exercise in both Python and JavaScript again! Today I’m doing Fibonacci numbers 🙂

Exercise: Fibonacci Sequence Generator

Objective: Write a program in both Python and JavaScript that generates and prints the first n numbers of the Fibonacci sequence. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1.

Requirements:

  1. Input: Your program should take an integer n as input, which represents the number of Fibonacci numbers to generate.
  2. Output: Your program should output the first n numbers of the Fibonacci sequence, each number printed on a new line.
  3. Validation: Include input validation to ensure n is a non-negative integer. If the input is invalid, your program should print a friendly error message.
  4. Efficiency: (Optional) Implement your solution efficiently to handle larger values of n gracefully.

My Code

Here my Python

def main():
    while True:
        try:
            user_input = int(input('n: '))
            if user_input <= 0:
                print('Please enter a positive number')
            else:
                break
        except ValueError:
            print('Please enter a number')
    fibonacci = 1
    helper = 0
    print('0')
    for _ in range(user_input - 1):
        print(fibonacci)
        new_fibonacci = fibonacci + helper
        helper = fibonacci
        fibonacci = new_fibonacci

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

And next my JavaScript, with browser interface!

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exercise: Fibonacci Sequence Generator</title>
</head>

<body>
    <input id="js-input" type="number">
    <button id="js-generate-button">generate</button>
    <p id="js-output"></p>

    <script>
        function makeFibonacci(number) {
            let array = [0]
            let fibonacci = 1
            let helper = 0
            let new_fibonacci = 0
            for (let i = number; i > 0; i--) {
                array.push(fibonacci)
                new_fibonacci = fibonacci + helper
                helper = fibonacci
                fibonacci = new_fibonacci
            }
            return array
        }

        document.getElementById('js-generate-button').onclick = function () {
            user_input = document.getElementById('js-input').value;
            output = makeFibonacci(user_input)
            document.getElementById('js-output').textContent = output;
        }
    </script>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Ohly thing I had to look up here is .push(). I should remember that by now. Also the output isn’t really pretty but I’ll work more on that another day

have a great way everyone!

Top comments (0)