DEV Community

tommy-3
tommy-3

Posted on

Learning Algorithms with JS, Python and Java 5: FizzBuzz

This is the fifth article of my attempts to follow Stephen Grider's Udemy course in three different languages.

When I wrote my last post a couple of days ago, I had only 5 followers, and now I have 35! I also received comments for the first time. I'm so happy I started this series, but now I feel kind of embarrassed to write about so famous a question at this timing.

There must be thousands of solutions, but here I just focus on showing two JS codes and trying to "translate" them into Python and Java as faithfully as possible.

--- Directions
Write a program that console logs the numbers
from 1 to n. But for multiples of three print
“fizz” instead of the number and for the multiples
of five print “buzz”. For numbers which are multiples
of both three and five print “fizzbuzz”.
--- Example
fizzBuzz(5);
1
2
fizz
4
buzz

1. Simple

JavaScript:

function fizzBuzz(n) {
    for (let i = 1; i <= n; i++) {
        if (i % 15 === 0) {
            console.log('fizzbuzz');
        } else if (i % 3 === 0) {
            console.log('fizz');
        } else if (i % 5 === 0) {
            console.log('buzz');
        } else {
            console.log(i);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Python:

def fizz_buzz(n):
    for i in range(1, n+1):
        if i % 15 == 0:
            print('fizzbuzz')
        elif i % 3 == 0:
            print('fizz')
        elif i % 5 == 0:
            print('buzz')
        else:
            print(i)
Enter fullscreen mode Exit fullscreen mode

Java:

public static void fizzBuzz(int n) {
    for (int i = 1; i <= n; i++) {
        if (i % 15 == 0) {
            System.out.println("fizzbuzz");
        } else if (i % 3 == 0) {
            System.out.println("fizz");
        } else if (i % 5 == 0) {
            System.out.println("buzz");
        } else {
            System.out.println(i);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2. More Concise

JavaScript:

function fizzBuzz(n) {
    for (let i = 1; i <= n; i++) {
        console.log((i % 3 ? '' : 'fizz')
            + (i % 5 ? '' : 'buzz') || i);
    }
}
Enter fullscreen mode Exit fullscreen mode

Python:

def fizz_buzz(n):
    for i in range(1, n+1):
        print(('' if i % 3 else 'fizz') +
              ('' if i % 5 else 'buzz') or i)
Enter fullscreen mode Exit fullscreen mode

Java:

public static void fizzBuzz(int n) {
    for (int i = 1; i <= n; i++) {
        String result = (i % 3 > 0 ? "" : "fizz") 
                + (i % 5 > 0 ? "" : "buzz");
        if (result.equals("")) {
            result = String.valueOf(i);
        }
        System.out.println(result);
    }
}
Enter fullscreen mode Exit fullscreen mode

I'm not really satisfied with the last Java code. I'd love to know if a more literal equivalent is possible. Thank you in advance for your comments!

Top comments (3)

Collapse
 
joefuzz profile image
Joe DuVall

I figured I would take a stab at the Java one, but I couldn't get it to be much more concise. Although, it gave me an opportunity to use IntStream which I recently discovered for myself.

public static void fizzBuzz(final int i) {
        IntStream.range(1, i + 1).forEachOrdered(n -> {
            final boolean divByThree = (n % 3) == 0;
            final boolean divByFive = (n % 5) == 0;
            System.out.print(
                    (divByThree ? "fizz" : "") +
                    (divByFive ? "buzz" : "") +
                    (!divByThree && !divByFive ? n : "") +
                    System.lineSeparator()        
            );
        });
    }

To me, it makes up for the lack of brevity by being more readable, but I recognize that this is more reflective of my style.

Collapse
 
tommy3 profile image
tommy-3

Thank you for your comment. I do like your style!

I'm not sure if it's any better, but we could also rewrite the print statement as:

System.out.println(divByThree || divByFive 
        ? (divByThree ? "fizz" : "") + (divByFive ? "buzz" : "") 
        : n);
Collapse
 
layzee profile image
Lars Gyrup Brink Nielsen

Check out my RxJS FizzBuzz implementation on StackBlitz:

stackblitz.com/edit/rxjs-fizzbuzz?...