Why JavaScript Beats PHP for Fizz Buzz

So I was talking to a friend about the classic programming test question, Fizz Buzz. I’ve actually had to answer it in two interviews, which is not a lot, but some people like to pull it out. It can be whiteboarded in about 5 minutes or less and tests for some basic skills.

If you’re not familiar with the problem:

Write a function that takes an argument. If that argument is divisible by 3, output “fizz.” If it’s divisible by 5, output “buzz.” If it’s divisible by both, output “fizz buzz.”

Now the easy answer to test for divisibility is if(x % y)===0. If the remainder of x divided by y is zero, then x is divisible by y.

But I’d been asked if there was another way to do this in one interview. PHP has a function is_int, so if(is_int(x/y)) works too.

But something occurred to me… if x is a floating point number, could you get wrong results in PHP?

With the modulus (%) solution, you can get all sorts of wrong results because the operands are converted to integers. 15.3 will pass and get a “fizz buzz” because it gets converted to 15.

If you go the is_int() route, it checks by the variable type, not the value. If either number in that equation is a floating point number, it will produce a floating point number. Thus if(is_int(15.0/3)) will fail. Even though the result is 5 and echoes as 5, it’s being stored in a non-integer data type.

What does that mean to Fizz Buzz? Well, with either method you have to sanitize your inputs to ensure they are integers and either throw an error/warning or skip them if they’re not, because 15.3 will pass the modulus test and 15.0 will fail the is_int() test, meaning either can produce the wrong results.

Update: Later That Evening

I was discussing this with a coder friend in chat and he pulls out the classic “well, of course, because PHP sucks.” So I whip open the console in Chrome and try some of these operations in JavaScript. It’s implemented a Number.isInteger() function I could substitute for is_int().

First, JavaScript doesn’t cast floating point numbers to integers in a modulus calculation. (3.4 % 2) produces 1.4 in JavaScript (try 3.2 % 2 and 3.3 % 2 at your own risk), but PHP would return 1. Also, since everything is a float, its Number.isInteger() sees 15.0 and any division by 3 or 5 as an integer, because it’s testing by value. You’re less likely to get false positives or negatives with JavaScript.

Conclusion JavaScript is a better language for Fizz Buzz than PHP. The programming interview will never be the same.

9 Replies to “Why JavaScript Beats PHP for Fizz Buzz”

  1. I hope that the next time you will study php before writing nonsense.
    function test( $x ){
    if( ( $x – (int)$x ) != 0 ) return null;
    if( is_int( $x / 3 ) == 0 ) echo “fizz\n”;
    if( is_int( $x / 5 ) == 0 ) echo “buzz\n”;
    }

    • Obviously there is a misprint:

      function test( $x ){
      $rest = $x – (int)$x;
      if( $rest != 0 ) return null;
      $x = (int)$x;
      if( is_int( $x / 3 ) ) echo “fizz\n”;
      if( is_int( $x / 5 ) ) echo “buzz\n”;
      }
      test( 15.0 );

      • A: You’re sanitizing your input to deal with the weakness of is_int, proving my point.

        B: You missed the case where both divisors hit. When both divisors hit, fizz & buzz should be separated by a space, not a linefeed.

        C: You’re doing the cast-to-integer operation twice when once would suffice with a better structure.

        • A: To not to be mistaken, you must choose the language you know. And since you choose your preferred language, surely you know how to deal with the language limitations. Javascript is not so better in certain conditions. Try to work with 64bit integers or try to write this in the javascript console and check the output:
          x = 9223372036854775807;
          or these:
          Math.pow(2,53);
          Math.pow(2,53) +1;
          Math.pow(2,53) +3;

          You would choose it unconditionally? Or you would try to use the best language to solve the problem?

          B & C: I wrote the code in 2 minutes just to prove that it is not so hard to do it in PHP.
          If you want, this is a better implementation ( yes, i wasted more time on this ):

          function test( $x ) {

          $int_x = (int)$x;
          if ( $x – $int_x ) return null;

          $int_div_3 = is_int( $int_x / 3 );
          $int_div_5 = is_int( $int_x / 5 );

          if ( $int_div_3 && $int_div_5 ) {
          echo “fizz buzz”;
          } elseif ( $int_div_3 ) {
          echo “fizz”;
          } elseif ( $int_div_5 ) {
          echo “buzz”;
          }

          }

          test( 15.0 );

Leave a Reply

Your email address will not be published. Required fields are marked *

*