Andy Jarrett // Code. Develop. Manage.

FizzBuzz in CFML and Javascript

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 in 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");
}
I’m here, learning and working away. If you liked this content and want to keep me going, consider buying me a coffee.
Your support keeps this site running and the coffee brewing! ☕️