borybar
Why do I get variable is unused when using it in case do?
Hi,
I am trying to apply different calculations depending on gender using ‘case do’ however I get variable "user_bmr" is unused. What am I not understanding?
def show(conn, %{"id" => id}, current_user) do
bmr = Cpm.get_user_bmr!(current_user, id)
case bmr.sex do
"Female" -> user_bmr = (9.99 * bmr.weight + 6.25 * bmr.height - 4.92 * bmr.age) - 161
"Male" -> user_bmr = (9.99 * bmr.weight + 6.25 * bmr.height - 4.92 * bmr.age) + 5
end
render(conn, "show.html", bmr: bmr, user_bmr: user_bmr)
end
Marked As Solved
unsek
case is an expression that “returns” the last line evaluated. You don’t have to put the user_bmr variable in case, since, if I’m not mistaken, it only exists in the scope of case. Instead you can do it this way:
user_bmr =
case bmr.sex do
"Female" -> (9.99 * bmr.weight + 6.25 * bmr.height - 4.92 * bmr.age) - 161
"Male" -> (9.99 * bmr.weight + 6.25 * bmr.height - 4.92 * bmr.age) + 5
end
render(conn, "show.html", bmr: bmr, user_bmr: user_bmr)
4
Popular in Questions
can someone please explain to me how Enum.reduce works with maps
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)?
Would
mix ecto.rollback -v 2008090...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I fore...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Other popular topics
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New







