DEV Community

loading...

Discussion on: Daily Challenge #48 - Facebook Likes

Collapse
aminnairi profile image
Amin

My take at the challenge written in Elm.

Source-code

module Main exposing (main)

import Html exposing (Html, p, text, div)

likes : List String -> String
likes names =
    case names of
        [] ->
            "no one likes this"

        name :: [] ->
            name ++ " likes this"

        first :: second :: [] ->
            first ++ " and " ++ second ++ " like this"

        first :: second :: third :: [] ->
            first ++ ", " ++ second ++ " and " ++ third ++ " like this"

        first :: second :: rest ->
            first ++ ", " ++ second ++ " and " ++ (String.fromInt <| List.length rest) ++ " others like this"

main : Html actions
main =
    div [] [ p [] [ text <| likes [] ] -- no one likes this
           , p [] [ text <| likes [ "Peter" ] ] -- Peter likes this
           , p [] [ text <| likes [ "Jacob", "Alex" ] ] -- Jacob and Alex like this
           , p [] [ text <| likes [ "Max", "John", "Mark" ] ] -- Max, John and Mark like this
           , p [] [ text <| likes [ "Alex", "Jacob", "Mark", "Max" ] ] -- Alex, Jacob and 2 others like this
           ]

Try it online

On Ellie App.