Elixir Today: Create a Downward Triangle Pattern using Elixir

Photo by Yingchih on Unsplash

Elixir Today: Create a Downward Triangle Pattern using Elixir

Pau Riosa's photo
Pau Riosa
ยทMay 7, 2022ยท

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Process

  • create a file named downward_triangle.ex
  • write the code
downward_triangle = fn number ->
  for i when i < number <- 0..number do
    for j when j < number - i <- 0..number do
      "*"
    end
  end
  |> Enum.into("", fn string ->
    string = Enum.join(string)
    "#{string}\n"
  end)
end

IO.puts(downward_triangle.(10))
IO.puts(downward_triangle.(5))
  • run elixir downward_triangle.ex

Result

**********
*********
********
*******
******
*****
****
***
**
*

*****
****
***
**
*

Happy Coding!

ย 
Share this