FizzBuzz in CFML

Reading an article on testing in interviews and it mentioned the Fizzbuzz test. If you haven't heard of this its simply:

  • Loop from 1 to 100.
  • When the number is divisible by 3 return Fizz
  • When the number is divisible by 5 return Buzz
  • When the number is divisible by both 3 & 5 return FizzBuzz

Saw this ages ago and wanted to have a play around with this myself (without googling) and do in this in CFML. Well here it is.

for(var i=1; i <= 100; i++){ writeOutput("("&i&")"&((i%3)?"":"fizz") & ((i%5)?"":"buzz")&"<br>"); }

There is probably a way to get it shorter though without hitting search this is my effort.

Also tried this Javascript and managed to get it a bit more lightweight thanks to console.log

for(var i=1; i <= 100; i++){ console.log(i,(i%3)?"":"fizz", (i%5)?"":"buzz"); }

comments powered by Disqus