Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/FizzBuzz.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import java.util.ArrayList;

public class FizzBuzz {
public static void main(String[] args) throws Exception {

ArrayList<String> result = new ArrayList<String>();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice this is a pretty good approach! For performance and memory reasons though, you might want to use a "StringBuilder" https://www.geeksforgeeks.org/stringbuilder-class-in-java-with-examples/

Since you're never doing any special array functions (like sorting, splicing etc)


for (int i = 1; i <= 100; i++) {

if ( i % 3 == 0 && i % 5 == 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);
if (i % 3 == 0) {
result.add("Fizz");
}
if (i % 5 == 0) {
result.add("Buzz");
}
if (result.size() == 0) {
result.add(String.valueOf(i));
}
Comment on lines +16 to +18
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a scenario in which I can introduce my favourite programming operator called the "ternary operator" https://www.w3schools.com/java/java_conditions_shorthand.asp

its perfect for when you want to do "if then else ". In this case it could be within the System.out.println you can check the length of the array/stringBuilder and return i if its 0, or the join if not


System.out.println(String.join("", result));

result.clear();
}
}
}