DEV Community

Cover image for 👨‍💻 Daily Code 43 | FizzBuzz Exercise in 🐍 Python and 🟨 JavaScript (round 2!)
Gregor Schafroth
Gregor Schafroth

Posted on

👨‍💻 Daily Code 43 | FizzBuzz Exercise in 🐍 Python and 🟨 JavaScript (round 2!)

Alright as promised, today I’ll try to do the FizzBuzz exercise again. I’ll try to make the solution elegant without looking at yesterdays solution.

My Code

🐍 Python:

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

🟨 JavaScript

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

Nice! Looks like I was able to nail it this time. The only further suggestions I get from ChatGPT on how to improve these is add comments and put them in a function, which are easy things to do.

Alright that’s it for today. Tomorrow I’ll do a new exercise again ⌨️ 😊

Top comments (0)