Colin Ragnarök
Colin Ragnarök

Reputation: 1062

How to map numbers of a list?

I have a list containing strings(players) and integers(strengths). I achieved to output the players with , div [] ( if model.activatedOutput then (List.map (\{ player} -> div [] [ text player ]) model.teams) else [] ) Now I also wanted to output the strength value stored in this record/list. But Get an argument error.

The 2nd argument to map is not what I expect:

130| , div [] ( if model.activatedOutput then (List.map ({ strength} -> div [] [ text strength ]) model.teams) else [] ) ^^^^^^^^^^^ The value at .teams is a:

List { activated : Bool, player : String, strength : Int }

But map needs the 2nd argument to be:

List { activated : Bool, player : String, strength : String }

I thought the error was due to the fact that strength has to be a string to map. But I had it converted into a String in the view. So I don't really get where this error comes from.

Here are some other parts of my code(The line actually causing the error is the last line in the code below):

-- MODEL
type alias Player =
  { player : String
  , strength : Int
  , activated : Bool
  }


type alias Model =
  { content : String
  , teams : List Player
  , currentPlayer : String
  , currentStrength : Int
  , activatedOutput : Bool
  }

-- UPDATE
...
 Add ->
      { model | teams = ({player = model.currentPlayer, strength = model.currentStrength, activated = True} :: model.teams), currentPlayer = "", currentStrength = 0 } 



init : Model
init =
  { content = ""
  , teams = []
  , currentPlayer = ""
  , currentStrength = 0
  , activatedOutput = False

   }
-- VIEW

view : Model -> Html Msg
view model =
  let
    playername = "🏅 Player " ++ String.fromInt (List.length model.teams + 1)
  in
  div []
    [ h1 [style "font-family" "impact"] [ text "Team Creator" ]
    , p [style "font-family" "sans-serif", style "font-size" "15px", style "color" "grey"] [ text "With the Team Creator you can create teams. Insert information about the name and the strength(1-5) of every player and finally how many teams you want to have created by the Team Creator" ]
    ,  h2 [style "font-family" "impact"] [ text "Number of Teams:" ]
    , input [ placeholder  "Number", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#32db64", value (String.fromInt model.currentNumber), onInput ChangeNumber] []
    ,  h2 [style "font-family" "impact"] [ text "Players per Team:" ]
    , input [ placeholder  "Playernumber", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#32db64", value (String.fromInt model.currentPlayernumber), onInput ChangePlayernumber] []
    ,  h2 [style "font-family" "impact"] [ text "Name and Strength:" ]
    , div[] [ input [placeholder  playername, style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#488aff", value model.currentPlayer, onInput ChangePlayer] [] ]
    , input [ placeholder  "💪🏼 Strength", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color"  "#4286F5", value (String.fromInt model.currentStrength), onInput ChangeStrength] []
    , div [] [ button [ style "background-color" "#66cc81", style "color" "white", style "margin-top" "20px", style "width" "300px", style "border-radius" "25px", style "height" "40px", style "font-size" "20px", style "margin-right" "70px", onClick Add] [ text "+ADD Player" ] ]
    , div [] [ button [ style "background-color" "#4286F5", style "color" "white", style "margin-top" "10px", style "width" "300px", style "border-radius" "25px", style "height" "40px", style "font-size" "20px", style "margin-right" "70px", onClick Submit] [ text "SUBMIT!" ] ]
    ,  h2 [style "font-family" "impact", style "margin-top" "20px"] [ text "Generated Teams:" ]
    , div [] ( if model.activatedOutput then (List.map (\{ player} -> div [] [ text player ]) model.teams) else [] )
    , div [] ( if model.activatedOutput then (List.map (\{ strength} -> div [] [ text strength ]) model.teams) else [] )
    ]

Upvotes: 0

Views: 249

Answers (1)

Jan Tojnar
Jan Tojnar

Reputation: 5524

HTML.text does not by itself convert numbers to Strings. It has type String -> Html msg, therefore it already expects the argument passed to it to be a String. You need to pass it through String.fromInt first.

Upvotes: 1

Related Questions