DEV Community

Discussion on: Get Your (Fizz)Buzz On

Collapse
 
danieljsummers profile image
Daniel J. Summers • Edited

From what I gleaned from people who had used this test "in the wild", the single most neglected step was "or else, print the number." Pretenders (or not-paying-attention-to-details developers) would get so wrapped up in all of the exceptions that they'd forget the "else" condition at the end.

Just for fun, here's one you probably didn't expect... COBOL 85 or after. (imagine column 1 starts at column 8 if your compiler cares about that...)

edit: Holy cow, this code formatter knows COBOL! Disregard the note above...

       identification division.
         program-id. fizzbuzz.
       environment division.
       data division.
         working-storage section.
       77  nbr pic 9(3) value zero.
       77  quotient pic 9(3) value zero.
       77  rem pic 9(2) value zero.
       procedure division.
       begin.
           perform varying nbr from 1 by 1 until nbr > 100
               divide nbr by 15 giving quotient remainder rem
               evaluate rem
                   when 0
                       display "FizzBuzz"
                   when 3
                   when 6
                   when 9
                   when 12
                       display "Fizz"
                   when 5
                   when 10
                       display "Buzz"
                   when other
                       display nbr
               end-evaluate
           end-perform.
           stop run.

This outputs 002 for 2, but I'm not posting a completely correct FizzBuzz solution on the public Internet. Interviewers don't want memorizers, they want programmers. :)

Collapse
 
yechielk profile image
Yechiel Kalmenson

Oh wow! I was NOT expecting a COBOL solution ;)

Guess that last part is left as an exercise for the reader (from my understanding, COBOL programmers don't have silly riddles in their interviews these days... :)

Collapse
 
danieljsummers profile image
Daniel J. Summers • Edited

I'm less than 4 years from the end of my current employment, and need to decide what to do next. Helping prop up aging COBOL is on my list to consider. :)

To bring it into this century, here's an F# version...

let words =
  function
  | _ when 0 = nbr % 3 && 0 = nbr % 5 -> "FizzBuzz"
  | _ when 0 = nbr % 3 -> "Fizz"
  | _ when 0 = nbr % 5 -> "Buzz"
  | x -> string x
let fizzBuzz i =
  [1 .. i]
  |> List.map words
  |> String.concat ", "
fizzBuzz 100
|> System.Console.WriteLine

...although, technically, this separates them by commas rather than listing one per line.