Coding Horror: Why Can't Programmers.. Program?

I just stumbled upon this and was shocked: Apparently programmers struggle even with the simplest of problems. What if I was one of them?

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Five minutes later I was relieved:

val fizzBuzz = (1 to 100).map(n =>
  if (n%3 == 0 && n%5 == 0) "FizzBuzz"
  else if (n%3 == 0) "Fizz"
  else if (n%5 == 0) "Buzz"
  else n
)

println(fizzBuzz.mkString("\n"))


© 2017. All rights reserved.

Powered by Hydejack v6.6.1