Exercise Weekend

The Ping-Pong test is a programming test that makes sure you understand how to use loops, conditionals, and variables. The challenge is to simply make a pingPong function that returns a numerically indexed array of all the numbers from 0 up to the number it was called on. But, for multiples of three, the array contains the string "ping" instead of the number, and for multiples of five, the array contains "pong". For numbers that are multiples of three and five, the array contains "ping-pong". 0 should remain 0. Remember that arrays are allowed to include both strings and numbers.

In other words:

> $returned_array = pingPong(18);
> var_dump($returned_array);
array(19) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
  [3]=>
  string(4) "ping"
  [4]=>
  int(4)
  [5]=>
  string(4) "pong"
  [6]=>
  string(4) "ping"
  [7]=>
  int(7)
  [8]=>
  int(8)
  [9]=>
  string(4) "ping"
  [10]=>
  string(4) "pong"
  [11]=>
  int(11)
  [12]=>
  string(4) "ping"
  [13]=>
  int(13)
  [14]=>
  int(14)
  [15]=>
  string(9) "ping-pong"
  [16]=>
  int(16)
  [17]=>
  int(17)
  [18]=>
  string(4) "ping"
}

Take the test and write the function! Write it out in a text file first so that you can edit it and make sure your formatting is correct. Be thorough: watch those curly brackets and indent your code. Then paste it into the PHP shell to test it.

Here is a hint: To tell if one number is divisible by another number, use the % operator, which is called modulo. Modulo gives you the remainder if you divide the number on the left by the number on the right. Try it out in the PHP shell.

> echo 14 % 7;
0
> echo 14 % 4;
2

14 % 7 evaluates to 0 because there's no remainder, and 14 % 4 evaluates to 2 because 14 / 4 is equal to 3 with a remainder of 2. Since anything non-zero is truthy and 0 is falsy, you can use the modulo in the condition of an if statement.