DEV Community

Cover image for 👨‍💻 Daily Code 42 | FizzBuzz Exercise in 🐍 Python and 🟨 JavaScript
Gregor Schafroth
Gregor Schafroth

Posted on

👨‍💻 Daily Code 42 | FizzBuzz Exercise in 🐍 Python and 🟨 JavaScript

Now that I finished my 🟨 JavaScript beginners course I want to get back to daily exercises from ChatGPT. Today I asked for a simple exercise that I can solve both with 🐍 Python and with 🟨 JavaScript, and I think I got a classic!

Let’s see if I am still up to it 🫣. Below are the exercise and then my code that I am about to write 😄

Exercise: FizzBuzz

  • Write a program that prints the numbers from 1 to 100.
  • For multiples of 3, print "Fizz" instead of the number.
  • For multiples of 5, print "Buzz" instead of the number.
  • For numbers which are multiples of both 3 and 5, print "FizzBuzz".

My Code

Alright I am starting off with Python. Even tho I haven’t used it in two weeks or so this is what I am most familiar with and expect to be the easiest

number = 1
while number <= 100:
    if number % 3 == 0 and number % 5 == 0:
        print('FizzBuzz')
    elif number % 3 == 0:
        print('Fizz')
    elif number % 5 == 0:
        print('Buzz')
    else:
        print(number)
    number = number + 1
Enter fullscreen mode Exit fullscreen mode

That was not too hard. Now JavaScript:

let number = 1;
while (number <= 100) {
    if (number % 3 === 0 && number % 5 === 0) {
        console.log('FizzBuzz')
    } else if (number % 3 === 0) {
        console.log('Fizz')
    } else if (number % 5 === 0) {
        console.log('Buzz')
    } else {
        console.log(number);
    }
    number++
}
Enter fullscreen mode Exit fullscreen mode

ChatGPT Feedback

Alright so my code works, but still I asked ChatGPT what I could have done better in my code. Here is what I got:

Python Improvements:

  1. Use for Loop: In Python, a for loop with a range is often more idiomatic than a while loop for fixed iterations like this. It makes the code a bit cleaner and more readable.
  2. String Concatenation: You can build the result string through concatenation. This avoids checking the combined condition (number % 3 == 0 and number % 5 == 0) explicitly, making the code slightly more elegant.

    for number in range(1, 101):
        result = ""
        if number % 3 == 0:
            result += "Fizz"
        if number % 5 == 0:
            result += "Buzz"
        print(result or number)
    

JavaScript Improvements:

  1. Use for Loop: Similar to Python, using a for loop can make your code more concise and clear.
  2. Template Literals for Debugging: Although not necessary for this exercise, if you want to add debugging or more complex strings in the future, template literals (${}) can be very useful.

    for (let number = 1; number <= 100; number++) {
        let result = "";
        if (number % 3 === 0) {
            result += "Fizz";
        }
        if (number % 5 === 0) {
            result += "Buzz";
        }
        console.log(result || number);
    }
    

My thoughts

Alright while my code works, I could have done this way more elegantly. I’ll give myself this same exercise again tomorrow and will see if I can just do the elegant solution myself then! 😄

Top comments (0)