Post solutions to the "Your Turns"?

I wonder what is the best way to learn.

I’m a beginning programmer. Elixir is my first programming language, I know HTML, CSS, GIT, I can deploy a website, set up Actions on GitHub, set Content Security Policies, etc etc. This all is mostly achieved by patching together and mimicking solutions found elsewhere. Note that I didn’t list JS there. I also have little experience in solving logical solutions within a programming language.

Programming Phoenix LiveView is listed as a sub-beginner to sub-expert skill level. It took me about 5 hours to complete the solution to the first “Your Turn” and don’t get me wrong I had fun doing it! The solution happend to contain subjects that where not explained in the chapter preceding. For an example handle_params/3 wasn’t discussed, this function is first mentioned at page 95. The only clue given is the live_patch/2 function. I really had to dig in there.

I didn’t look for solutions for this challenge, there aren’t any posted here, but if there where I would be tempted to copy them. But on the other side I also want feedback on my solutions, I want to learn the best way to solve a problem. I learned a lot by digging in, not copying solutions. But is took a lot of effort and I wonder if the average reader will be able to persevere. Question is what is the average reader you are aiming at? And what is the best way to learn?

Anyhow here is my solution to the first Your Turn.

“Assign a random number”
This was an easy find.

def random_number() do
  Enum.random(1..10) |> to_string
end

Add that to socket assigns

random_number: random_number()

“Check for that number in the handle_event”.
This was the most difficult part. You know you have to compare the random number to the guess, update the message and update the score. I found 2 ways doing that, I prefer the second. Are there any better ways?

# message =
#   if guess == correct_answer do
#     "Your guess: #{guess}. Right! You win!"
#   else
#     "Your guess: #{guess}. Wrong. Guess again."
#   end

# score =
#   if guess == correct_answer do
#     socket.assigns.score + 1
#   else
#     socket.assigns.score - 1
#   end

message =
  cond do
    guess == correct_answer -> "Your guess: #{guess}. Right! You win!"
    guess != correct_answer -> "Your guess: #{guess}. Wrong. Guess again."
  end

score =
  cond do
    guess == correct_answer -> socket.assigns.score + 10
    guess != correct_answer -> socket.assigns.score - 1
  end

“Award points for a right guess.”
This is where confusion about the games mechanics mingles in. My solution, as you can see above, was to award a correct guess with 10 points. Given there are 10 guesses possible, after 9 guesses the final score is 1. But a user can click on a solution more than once, so their score will drop below 1. I left this unsolved.

“Show a restart message and button when the user wins.”

 <%= if @status == :wins do %>
  <%= live_patch to: Routes.live_path(@socket, __MODULE__), replace: true do %>
   <button>Try again!</button>
   <% end %>
 <% end %>

I introduced a “playing status”, someone is playing or wins. I added that to the socket assigns.

 status =
   if guess == correct_answer do
     :wins
   else
     :playing
   end