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)
🟨 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);
}
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)