Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

MetaMath

exercises/meta_math.livemd

MetaMath

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue MetaprogrammingCustom Assertions

MetaMath

You’re creating a math game. Players will be given math questions in the format "2 + 2".

The string can contain multiple operations in any order such as "2 + 10 * 23 - 10"

You need to determine the correct result to check with the player’s answer.

Math.correct?("2 + 2", 4) # true 
Math.correct?("2 + 2", 5) # false

Example Solution

defmodule Math do
  def correct?(equation, player_answer) do
    {result, _bindings} = Code.eval_string(equation)
    result == player_answer
  end
end

Implement the Math module as documented.

defmodule Math do
  @doc """
  Determine if a player's answer matches the result of the math expression in the string.

  iex> Math.correct?("1 + 1", 2)
  true
  iex> Math.correct?("1 + 1", 4)
  false
  iex> Math.correct?("1 - 1", 0)
  true
  iex> Math.correct?("10 + 14 * 4 - 2", 64)
  true
  """
  def correct?(equation, player_answer) do
  end
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish MetaMath exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue MetaprogrammingCustom Assertions