DEV Community

loading...

Discussion on: Daily Challenge #48 - Facebook Likes

Collapse
thepeoplesbourgeois profile image
Josh • Edited

I question the validity of this challenge. No Oxford comma??? 🧐

But on with it...

defmodule Liked do
  def by([]), do: "no one likes this"
  def by([friend]), do: "#{friend} likes this"
  def by([friend_1, friend_2]), do: "#{friend_1} and #{friend_2} like this"
  def by([f1, f2, f3]), do: "#{f1}, #{f2} and #{f3} like this"
  def by([friend_1, friend_2 | friends]), do: "#{friend_1}, #{friend_2} and #{length(friends)} others like this"

end

Liked.by([])
# "no one likes this"
Liked.by(["the strippers"])
# "the strippers like this"
Liked.by(["the strippers", "JFK"])
#"the strippers and JFK like this"
Liked.by(["the strippers", "JFK", "Stalin"])
# "the strippers, JFK and Stalin like this
america = List.duplicate("american", 300000000) # p expensive, don't actually make this large of a list
Liked.by(["the strippers", "JFK", "Stalin" | america])
# "the strippers, JFK and 300000001 others like this"