- Edited
for i in [ "fizz" if i%3==0 and i%5!=0 else " buzz" if i%5==0 and i%3!=0 else "fizzbuzz" if i%3==0 and i%5==0 else str(i) for i in xrange(1,101)]: print i
I thought I'd share this one liner.for i in [ "fizz" if i%3==0 and i%5!=0 else " buzz" if i%5==0 and i%3!=0 else "fizzbuzz" if i%3==0 and i%5==0 else str(i) for i in xrange(1,101)]: print i
I thought I'd share this one liner.package main
import (
"fmt"
"strconv"
)
func fizzbuzz(n int) string {
var result string
for i := 0; i <= n; i++ {
if i%3 == 0 && i%5 == 0 {
result = "fizzbuzz"
} else if i%3 == 0 {
result = "fizz"
} else if i%5 == 0 {
result = "buzz"
} else {
result = strconv.Itoa(i)
}
fmt.Printf("%s\n", result)
}
return result
}
func main() {
fizzbuzz(20)
}
console.log((new Array(100)).join(0).split('').map(function(v,i){return(((i+1)%3==0)&&((i+1)%5==0)?"FizzBuzz":((i+1)%3==0)?"Fizz":((i+1)%5==0)?"Buzz":(i+1));}).join("\n"));
Since Array.map() would ignore undefined values, I have to do join() then split() to end up with an array of zeroes (or anything else than undefined). Anyone knows a better way of doing it in javascript?