Last active
August 4, 2022 22:58
-
-
Save KevinDaSilvaS/2121902fe071977629a4cef85f2948f9 to your computer and use it in GitHub Desktop.
fizz_buzz
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
defmodule FizzBuzz do | |
def start(amount), do: 1..amount |> Enum.map(&fizz_buzz(&1)) | |
def fizz_buzz(num) do | |
dividedByThree = rem(num, 3) == 0 | |
dividedByFive = rem(num, 5) == 0 | |
cond do | |
dividedByThree && dividedByFive -> "FizzBuzz" | |
dividedByThree -> "Fizz" | |
dividedByFive -> "Buzz" | |
true -> num | |
end | |
end | |
end | |
IO.inspect FizzBuzz.start(5) |
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
def start(amount): | |
input = list(range(1, amount+1)) | |
return map(fizz_buzz, input) | |
def fizz_buzz(num): | |
final = "" | |
if(num%3 == 0): | |
final += "Fizz" | |
if(num%5 == 0): | |
final += "Buzz" | |
if(final == ""): | |
return num | |
return final | |
print(list(start(5))) |
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
fn start(amount int) []string { | |
input := []int{len: amount, init: it+1} | |
return input.map(fizz_buzz) | |
} | |
fn fizz_buzz(num int) string { | |
return match true { | |
num%3 == 0 && num%5 == 0 { 'FizzBuzz' } | |
num%3 == 0 { 'Fizz' } | |
num%5 == 0 { 'Buzz' } | |
else { '$num' } | |
} | |
} | |
fn main() { | |
println(start(5)) | |
} |
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
main = do | |
putStrLn "Hello, World!" | |
print $ start 5 | |
start amount = map fizzBuzz [1..amount] | |
fizzBuzz num | |
| dividedByThree && dividedByFive = "FizzBuzz" | |
| dividedByThree = "Fizz" | |
| dividedByFive = "Buzz" | |
| otherwise = show num | |
where | |
dividedByThree = num `rem` 3 == 0 | |
dividedByFive = num `rem` 5 == 0 | |
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
const start = amount => { | |
const output = [] | |
for (let index = 1; index < amount+1; index++) { | |
output.push(fizzBuzz(index)) | |
} | |
return output | |
} | |
const fizzBuzz = num => { | |
let final = "" | |
if(num%3 == 0) | |
final += "Fizz" | |
if(num%5 == 0) | |
final += "Buzz" | |
if(final == "") | |
return num | |
return final | |
} | |
console.log(start(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment