fizzbuzz
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
main =
beginnerProgram
{ model = model,
update = update,
view = view
}
-- MODEL
type alias Model =
{ max: Int,
fizz: Int,
buzz: Int
}
model : Model
model = { max = 100, fizz = 3, buzz = 5 }
-- UPDATE
type Msg
= Max String
| Fizz String
| Buzz String
update : Msg -> Model -> Model
update msg model =
case msg of
Max max ->
{ model | max = Result.withDefault 0 (String.toInt max) }
Fizz fizz ->
{ model | fizz = Result.withDefault 0 (String.toInt fizz) }
Buzz buzz ->
{ model | buzz = Result.withDefault 0 (String.toInt buzz) }
-- VIEW
view : Model -> Html Msg
view model =
div [] [
div [class "fizzbuzzInputs"]
[
maxView model.max,
fizzbuzzInputView model.fizz model.buzz
],
fizzbuzzView model
]
maxView : Int -> Html Msg
maxView max =
input [ type_ "number", value (toString max), onInput Max ] []
fizzbuzzInputView : Int -> Int -> Html Msg
fizzbuzzInputView fizz buzz =
div [ class "fizzbuzzInputs" ] [
div [ id "fizzContainer" ] [
label [] [text "Fizz"],
input [ type_ "number", value (toString fizz), onInput Fizz ] []
],
div [ id "buzzContainer" ] [
label [] [text "Buzz"],
input [ type_ "number", value (toString buzz), onInput Buzz ] []
]
]
fizzbuzzView : Model -> Html msg
fizzbuzzView model =
let
fizzbuzzElm n = li [ ] [ text n ]
fizzbuzzElms =
fizzbuzz model.max model.fizz model.buzz |>
List.map fizzbuzzElm
in
ul [ class "fizzBuzzList" ] fizzbuzzElms
fizzbuzz : Int -> Int -> Int -> List String
fizzbuzz max fizz buzz =
let
fizzbuzzStr n =
case (n % fizz, n % buzz) of
(0, 0) -> "FizzBuzz"
(0, _) -> "Fizz"
(_, 0) -> "Buzz"
_ -> toString n
in
List.range 1 max |>
List.map fizzbuzzStr