Skip to content
Open
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions src/main/java/org/fundacionjala/coding/sergio/FizzBuzz.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.fundacionjala.coding.sergio;

/**
* Created by SergioNavarro on 8/25/2017.
*/
public class FizzBuzz {

/**
* @param num input.
* @return return.
*/
public String[] resolve(int num) {
String[] result = new String[num];
boolean normal = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need this flag ?
I don't think so
Please change the logic and remove it.

for (int i = 1; i <= num; i++) {
normal = true;
if (i % 3 == 0 && i % 5 == 0) {
result[i - 1] = "FizzBuzz";
normal = false;
}
if (i % 3 == 0 && normal) {
result[i - 1] = "Fizz";
normal = false;
}
if (i % 5 == 0 && normal) {
result[i - 1] = "Buzz";
normal = false;
}
if (normal) {
result[i - 1] = Integer.toString(i);
}
}
return result;
}
}
35 changes: 35 additions & 0 deletions src/test/java/org/fundacionjala/coding/sergio/FizzBuzzTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.fundacionjala.coding.sergio;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;

import static junit.framework.TestCase.assertTrue;

/**
* Created by SergioNavarro on 8/25/2017.
*/
public class FizzBuzzTest {
private FizzBuzz fizzBuzz;

/**
* Initial setup.
*/
@Before
public void setUp() {
fizzBuzz = new FizzBuzz();
}

/**
*
*/
@Test
public void test01() {
int num = 15;
String[] input = {"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14",
"FizzBuzz"};
assertTrue(Arrays.equals(input, fizzBuzz.resolve(num)));
}

}