Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 1.54 KB

introduction.md

File metadata and controls

49 lines (39 loc) · 1.54 KB

Introduction

Check every possibility

The output of the convert method depends on three conditions which can be either true or false. This gives only eight possibilities and we can check them all.

def convert(number) do
  case {rem(number, 3), rem(number, 5), rem(number, 7)} do
    {0, 0, 0} -> "PlingPlangPlong"
    {0, 0, _} -> "PlingPlang"
    {0, _, 0} -> "PlingPlong"
    {_, 0, 0} -> "PlangPlong"
    {0, _, _} -> "Pling"
    {_, 0, _} -> "Plang"
    {_, _, 0} -> "Plong"
    _ -> Integer.to_string(number)
  end
end

We can use a few Elixir features to do more or less the same and we explore them in the check every possibility approach.

Step by step

An alternative approach is to consider each condition one at a time. At each step we return either a sound (i.e. "Pling", "Plang", or "Plong"), or an empty string. We can then concatenate the strings together.

def convert(number) do
  pling = if rem(number, 3) == 0, do: "Pling", else: ""
  plang = if rem(number, 5) == 0, do: "Plang", else: ""
  plong = if rem(number, 7) == 0, do: "Plong", else: ""
  result = pling <> plang <> plong

  if result == "" do
    Integer.to_string(number)
  else
    result
  end
end

Let's have a look at a few variations of this step by step approach.