Muco

Muco

Hello, everyone, I’m new to Elixir I’m reading a book entitled Phoenix in Action I’m on its second chapter where the author introduces to Elixir but I’m facing this error when I run it into the Elixir shell. This is here:

iex(5)> Fizzbuzz.go(1, 100)
** (CondClauseError) no cond clause evaluated to a truthy value
    (fizzbuzz 0.1.0) lib/fizzbuzz.ex:11: Fizzbuzz.show_results/1
    (elixir 1.10.1) lib/enum.ex:789: anonymous fn/3 in Enum.each/2
    (elixir 1.10.1) lib/enum.ex:3371: Enum.reduce_range_inc/4
    (elixir 1.10.1) lib/enum.ex:2116: Enum.each/2

And the code is here:

defmodule Fizzbuzz do
  def go(minNumber, maxNumber) do
    Enum.each(minNumber..maxNumber, fn number -> show_results(number) end)
  end

  def show_results(number) do
    # IO.puts(number)
    cond do
      rem(number, 3) == 0 && rem(number, 5) == 0 -> IO.puts("FizzBuzz")
      rem(number, 3) == 0 -> IO.puts("Fizz")
      rem(number, 5) == 0 -> IO.puts("buzz")
    end
  end
end

But in the show_results function when I just add IO.puts(number) only I can see them in the console. I need your help, please.

Showing Posts 1 to 5

kip

kip

ex_cldr Core Team

The message is telling you that there are no conditions within the cond expression that evaluate to truthy. What should a cond do if no conditions are met? it raises an exception :slight_smile:

The usual approach is to add a final condition that is guaranteed to evaluate to a truthy value. Often true. So:

     cond do
      rem(number, 3) == 0 && rem(number, 5) == 0 -> IO.puts("FizzBuzz")
      rem(number, 3) == 0 -> IO.puts("Fizz")
      rem(number, 5) == 0 -> IO.puts("buzz")
      true -> IO.puts("Nothing to see here")
    end
Muco

Muco OP

Thank you @kip for your response.

The thing that I’m not understanding is why there’s no truthy evaluated as I can see I have numbers from 1 to 100

Muco

Muco OP

Aah!! I actually got it so it means if it encounters nothing evaluated it will just stop the program and raise the exception ? Thank you again @kip

ityonemo

ityonemo

exactly. Also just be careful, the catch-all for cond is true, and the catch-all for case is _ (or any variable), It’s easy to get confused if you refactor one to the other.

chrisdel101

chrisdel101

I had this same question. The first run of cond had no truthy values so gave an error. Adding a default end condition using the true -> "do stuff" solved it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews