Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

> Fizz Buzz, any languages

views
     
nyem
post May 18 2007, 09:30 AM

Enthusiast
*****
Senior Member
721 posts

Joined: Jan 2007


FizzBuzz in javascript
CODE
<html>
<body>
    <script>

    for (var i=1;i<=100;i++)     {
         document.write(
              ((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>'
         )
    }

    </script>
</body>
</html>


nyem
post May 19 2007, 01:48 AM

Enthusiast
*****
Senior Member
721 posts

Joined: Jan 2007


QUOTE(nyem @ May 18 2007, 09:30 AM)
FizzBuzz in javascript
CODE
<html>
<body>
    <script>

    for (var i=1;i<=100;i++)     {
         document.write(
              ((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>'
         )
    }

    </script>
</body>
</html>

*
just realised that I can execute the javascript by pasting it into the browser address bar, no need to embed in html.

CODE
javascript:for(var i=1;i<=100;i++)document.write(((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>');void(0);



nyem
post May 23 2007, 02:53 AM

Enthusiast
*****
Senior Member
721 posts

Joined: Jan 2007


QUOTE(bluefox @ May 22 2007, 11:33 PM)
"19142723" why that number?? was it some shifting binary or hexa?? i really can't depict that...
*

I'm also at a loss. The magic number also work in javascript
CODE
var T = [0,'Fizz','Buzz','FizzBuzz'];
for (var i=1;i<=100;i++)     {
    T[0] = i;
    document.write( T[ 3&19142723>>2*i%30 ] + '<br>' );
}


and perl
CODE
perl -le "print+($_,Fizz,Buzz,FizzBuzz)[3&19142723>>2*$_%30]for 1..100"


The only explanation from Samuel Tardieu
QUOTE
Olive: I would only give the recruiter this kind of answer if I knew that she would appreciate it (being an ügergeek for example). The trick is to represent characteristics of every number (modulo 15) by an integer between 0 and 3 (plain, fizz, buzz, fizzbuzz). You need two bits for each number between 0 and 15, and can generate a decimal number with those 30 bits with: (thanks to J)
  4#.|.(-.*3|i.15)++:-.*5|i.15
19142723
(no, I don’t feel like explaining J today)

Then it’s simply a matter of choosing between the four alternatives after right shifting this large number by twice i (modulo 15) and using the two rightmost bits only.


 

Switch to:
| Lo-Fi Version
0.0188sec    0.34    6 queries    GZIP Disabled
Time is now: 2nd April 2020 - 08:51 AM