-3
public String[] fizzBuzz(int start, int end) {
int end1 = end - start;
String [] new1 = new String[end]; 
for(int i = start; i < end; i++)
{
  if(i % 3 == 0 && i % 5 == 0)
  {
    new1[i] = "FizzBuzz";
  }
  else if(i % 3 == 0)
  {
    new1[i] = "Fizz";
  }
  else if (i % 5 == 0)
  {
    new1[i] = "Buzz";
  }
  else
  {
    new1[i] = Integer.toString(i);
  }
}
return new1;
}

EXAMPLE:

Expected:

fizzBuzz(1, 6) → ["1", "2", "Fizz", "4", "Buzz"]

Result:

[null, "1", "2", "Fizz", "4", "Buzz"]
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Arrays indices start from 0. Calling `fizzBuzz(1, 6)` will start iteration at index 1. – Andrew Li Jan 31 '18 at 23:51
  • @Li357 I think that your comment answers the question very well. If you decide to write it as an official answer, other users will be able to find it more easily. :) – Keara Jan 31 '18 at 23:53

1 Answers1

0

When you call the function fizzBuzz(1, 6), the last condition i.e, new1[i] = Integer.toString(i); will be executed. where i = 1 which is the second index of array new1 since array indexes start from 0. So obviously, the first index of the array i.e,new[0] is null.

Vyshak
  • 126
  • 1
  • 9