Last active
July 20, 2016 03:09
-
-
Save nawada/73ff72084779f34a45b1956400fe436c to your computer and use it in GitHub Desktop.
FizzBuzz Code Golf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ozy? http://d.hatena.ne.jp/Ozy/20070225#p1 | |
for(i=0;++i<101;console.log(i%5?x||i:x+'Buzz'))x=i%3?'':'Fizz' | |
// Basic | |
for(var i = 1; i <= 100; i++) { | |
if(i % 15 === 0) { | |
console.log('fizzbuzz'); | |
} else if(i % 3 === 0) { | |
console.log('fizz'); | |
} else if(i % 5 === 0) { | |
console.log('buzz'); | |
} else { | |
console.log(i); | |
} | |
} | |
// Remove var | |
for(i = 1; i <= 100; i++) { | |
if(i % 15 === 0) { | |
console.log('fizzbuzz'); | |
} else if(i % 3 === 0) { | |
console.log('fizz'); | |
} else if(i % 5 === 0) { | |
console.log('buzz'); | |
} else { | |
console.log(i); | |
} | |
} | |
// change operator | |
for(i = 1; i < 101; i++) { | |
if(i % 15 == 0) { | |
console.log('fizzbuzz') | |
} else if(i % 3 == 0) { | |
console.log('fizz') | |
} else if(i % 5 == 0) { | |
console.log('buzz') | |
} else { | |
console.log(i) | |
} | |
} | |
// shorten console.log | |
c = console | |
c.l = c.log | |
for(i = 1; i < 101; i++) { | |
if(i % 15 == 0) { | |
c.l('fizzbuzz') | |
} else if(i % 3 == 0) { | |
c.l('fizz') | |
} else if(i % 5 == 0) { | |
c.l('buzz') | |
} else { | |
c.l(i) | |
} | |
} | |
// remove if | |
c = console | |
c.l = c.log | |
for(i = 1; i < 101; i++) { | |
i % 15 == 0 ? c.l('fizzbuzz') : i % 3 == 0 ? c.l('fizz') : i % 5 == 0 ? c.l('buzz') : c.l(i) | |
} | |
// remove spaces, crlf | |
c=console;c.l=c.log;for(i=1;i<101;i++)i%15==0?c.l('fizzbuzz'):i%3==0?c.l('fizz'):i%5==0?c.l('buzz'):c.l(i) | |
// remove for condition | |
c=console;c.l=c.log;for(i=0;++i<101;)i%15==0?c.l('fizzbuzz'):i%3==0?c.l('fizz'):i%5==0?c.l('buzz'):c.l(i) | |
// my best code | |
for(i=0;++i<101;)console.log((i%3?'':'fizz')+(i%5?'':'buzz')||i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Shortest? | |
1.upto(100){|n|puts'FizzBuzz | |
'[i=n**4%-15,i+13]||n} | |
# Basic | |
(1..100).each { |i| | |
if i % 15 == 0 | |
puts 'fizzbuzz' | |
elsif i % 3 == 0 | |
puts 'fizz' | |
elsif i % 5 == 0 | |
puts 'buzz' | |
else | |
puts i | |
end | |
} | |
# use modifier if | |
(1..100).each { |i| | |
s = 'fizz' if i % 3 == 0 | |
s = 'buzz' if i % 5 == 0 | |
s = 'fizzbuzz' if i % 15 == 0 | |
puts s || i | |
} | |
# use variable | |
(1..100).each { |i| | |
s = '' | |
s = 'fizz' if i % 3 == 0 | |
s << 'buzz' if i % 5 == 0 | |
puts s.empty? ? i : s | |
} | |
# remove spaces, crlf | |
(1..100).each{|i|s='';s='fizz'if i%3==0;s<<'buzz'if i%5==0;puts s.empty? ? i:s} | |
# use Integer#upto | |
1.upto(100){|i|s='';s='fizz'if i%3==0;s<<'buzz'if i%5==0;puts s.empty? ? i:s} | |
# change operator ( ==0 -> <1) | |
1.upto(100){|i|s='';s='fizz'if i%3<1;s<<'buzz'if i%5<1;puts s.empty? ? i:s} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment