Interview like it's the 2020's... (FizzBuzz in Elm 0.19)
0
fizzy.elm
1import Browser
2import Html exposing (..)
3import Html.Attributes exposing (..)
4import Html.Events exposing (onInput)
5import String
6
7
8main =
9 Browser.sandbox { init = 0, update = update, view = view }
10
11
12type Msg
13 = Change String
14
15
16update : Msg -> Int -> Int
17update (Change upperLimit) model =
18 Maybe.withDefault 0 (String.toInt upperLimit)
19
20
21view : Int -> Html Msg
22view model =
23 div [ style "margin" "10px" ]
24 [ h1 [] [ text "Fizz Buzz" ]
25 , label [ style "font-weight" "bold", for "upperLimit" ] [ text "Upper limit:" ]
26 , input
27 [ style "margin-left" "10px"
28 , style "margin-bottom" "10px"
29 , id "upperLimit"
30 , type_ "text"
31 , onInput Change
32 , size 3
33 , maxlength 5
34 , value (
35 case model of
36 0 -> ""
37 _ -> String.fromInt model
38 )
39 ]
40 [ text (String.fromInt model) ]
41 , div [] [ text (fizzBuzz model) ]
42 ]
43
44
45fizzBuzz : Int -> String
46fizzBuzz upperLimit =
47 List.range 1 upperLimit
48 |> List.map
49 (\x ->
50 let
51 mods =
52 ( (modBy 3 x) == 0, (modBy 5 x) == 0 )
53 in
54 case mods of
55 ( True, True ) ->
56 "FizzBuzz"
57
58 ( True, False ) ->
59 "Fizz"
60
61 ( False, True ) ->
62 "Buzz"
63
64 _ ->
65 String.fromInt x
66 )
67 |> String.join ", "