# Thread: Building Fizz Buzz in JavaScript!

1. ## Building Fizz Buzz in JavaScript!

If your boss asked you could you build a fizz buzz program in javascript could you? Something so simple that alot of great programmers and coders can't do. Well I want to see what you guys comes up with! Also for those that just want to see how its done take a look here: http://pbswebdesign.blogspot.com/201...avascript.html

This is intended to see how people do and how differently they do it. TEst yourself and see if you can still remember the basics!

Here is what needs to be done!
Count from 1 to 100 replacing any number divisible by 3 with the word "Fizz" and any number divisible by 5 with "Buzz". Now if the number is both divisible by 3 and 5 then we need to display "FizzBuzz". If the number is not divisible by 3 or 5 we need to just output that number.
Last edited by PBSWebDesign; 04-01-2012 at 03:35 PM.

2. Code:
```<script type="application/javascript">

(function ()
{
var str = "";

for( var i = 1, dv3 = 1, dv5 = 1; i < 101; i++, dv3 += ( dv3 == 2 ? -dv3 : 1 ), dv5 += ( dv5 == 4 ? -dv5 : 1 ) )
str +=  ( !dv3 && !dv5 ? "FizzBuzz" : !dv3 ? 'Fizz' : !dv5 ? 'Buzz' : i ) + ' ';

document.write( str );
})();

</script>```
I'm hired!

3. Very nice! I also ran across this solution too online:

Code:
`for(i=0;++i<101;)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)`

4. Originally Posted by PBSWebDesign
Very nice! I also ran across this solution too online:

Code:
`for(i=0;++i<101;)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)`
That's fine, but I wanted an alternative to performing repeated division.