This is from the exercise of Phoenix Liveview. Can someone tell me why is guess
a string? (the guess is in the handle_event function) And how to make it a number instead? Thank you
defmodule PentoWeb.WrongLive do
use PentoWeb, :live_view
def mount(_params, _session, socket) do
{:ok, assign(
socket,
score: 0,
message: "Make a guess:",
time: time(),
answer: rand_num()
)
}
end
@spec render(any) :: Phoenix.LiveView.Rendered.t()
def render(assigns) do
~H"""
<h1>Your score: <%= @score %></h1>
<h2>
<%= @message %>
It's <%= @time %>
The answer is <%= @answer %>
</h2>
<h2>
<%= for n <- 1..10 do %>
<.button phx-click="guess" phx-value-number= {n} >
<%= n %>
</.button>
<% end %>
</h2>
"""
end
def time() do
DateTime.utc_now |> to_string
end
def rand_num() do
Enum.random(0..10)
end
def handle_event("guess", %{"number" => guess}, socket) do
time = time()
answer = socket.assigns.answer
guess_num = String.to_integer(guess) #IDK, it is fucking string
if (guess_num == answer) do
message = "Congratuations! Your guess: #{guess} is correct. "
score = socket.assigns.score + 1
{
:noreply,
assign(
socket,
message: message,
score: score,
time: time,
answer: answer
)
}
else
message = "Your guess: #{guess}. Wrong. Guess again. "
score = socket.assigns.score - 1
{
:noreply,
assign(
socket,
message: message,
score: score,
time: time,
answer: answer
)
}
end
end
end