Last active
August 17, 2019 18:00
-
-
Save JesterXL/40899983f828aa41cb75ba5bdf2706af to your computer and use it in GitHub Desktop.
FizzBuzz solutions in JavaScript and Elm.
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
// JavaScript | |
const fizzAndOrNotBuzz = i => { | |
switch(true) { | |
case i % 15 === 0: return 'FizzBuzz' | |
case i % 5 === 0: return 'Buzz' | |
case i % 3 === 0: return 'Fizz' | |
default: return i | |
} | |
} | |
Array(100) | |
.fill(0) | |
.map( (_, index) => index + 1 ) | |
.map( item => fizzAndOrNotBuzz(item) ) | |
.map( item => console.log(item) ) | |
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
module Main exposing (main) | |
import Browser | |
import Html exposing (Html, button, div, text, textarea) | |
import Html.Attributes exposing (cols, rows) | |
import Html.Events exposing (onClick) | |
import List exposing (foldl, indexedMap, map, repeat) | |
import String exposing (toInt) | |
type alias Model = | |
{ result : String } | |
fizzAndOrNotBuzz : Int -> String | |
fizzAndOrNotBuzz i = | |
if modBy 15 i == 0 then"FizzBuzz" | |
else if modBy 5 i == 0 then "Buzz" | |
else if modBy 3 i == 0 then "Fizz" | |
else String.fromInt i | |
doFizzBuzz : String | |
doFizzBuzz = | |
repeat 100 1 | |
|> indexedMap (\item index -> item + index) | |
|> map fizzAndOrNotBuzz | |
|> foldl (\item acc -> acc ++ item ++ "\n") "" | |
initialModel : Model | |
initialModel = | |
{ result = "Ready?" } | |
type Msg | |
= PassInterview | |
update : Msg -> Model -> Model | |
update msg model = | |
case msg of | |
PassInterview -> | |
{ model | result = doFizzBuzz } | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ button [ onClick PassInterview ] [ text "Pass Interview" ] | |
, textarea [ rows 4, cols 50 ] [ text model.result ] | |
] | |
main : Program () Model Msg | |
main = | |
Browser.sandbox | |
{ init = initialModel | |
, view = view | |
, update = update | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment