www.webdeveloper.com
Results 1 to 4 of 4

Thread: Building Fizz Buzz in JavaScript!

  1. #1
    Join Date
    Feb 2011
    Location
    Chuluota, Florida
    Posts
    144

    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.
    ----------------------------------------------------------------------------------------------
    WYSIWYG editors will never beat my hand written code!!!! Learn to do it in notepad and learn how to actually control your website! Current Project http://www.jmcanineservices.com

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    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!
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Feb 2011
    Location
    Chuluota, Florida
    Posts
    144
    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)
    ----------------------------------------------------------------------------------------------
    WYSIWYG editors will never beat my hand written code!!!! Learn to do it in notepad and learn how to actually control your website! Current Project http://www.jmcanineservices.com

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by PBSWebDesign View Post
    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.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles