Hi Team,
I have been doing some test for a school project for a math’s function, Need help I am trying to create a function for adding numbers with steps returned as JSON output.
using Genie, Genie.Router, Genie.Renderer.Json, Genie.Requests
include("addfun.jl")
# Define the route for the POST request
route("/addstep", method = POST) do
# Parse JSON payload
payload = Genie.Requests.jsonpayload()
# Extract the list of numbers
numbers = payload["numbers"]
# Perform the addition (sum of the numbers)
result = add_and_display(numbers)
# Return the result as JSON
return Json.json(Dict("result" => result))
end
the below add function
using JSON
function add_and_display(numbers::Vector{<:Union{Int, Float64}})
# Initialize variables to store steps
steps = []
# Step 1: Sum the numbers
sum_numbers = sum(numbers)
# Step 2: Store each number
push!(steps, "Numbers:")
for num in numbers
push!(steps, string(num))
end
# Step 3: Store the sum
push!(steps, "\nSum:")
push!(steps, string(sum_numbers))
# Step 4: Store the solution steps
push!(steps, "\nSolution Steps:")
for i in 1:length(numbers)-1
step_description = "Step $i: $(sum(numbers[1:i])) + $(numbers[i+1]) = $(sum(numbers[1:i+1]))"
push!(steps, step_description)
end
# Return the steps and result as a dictionary for JSON serialization
result = Dict("sum" => sum_numbers, "steps" => steps)
return result
end
for above I am getting error.
julia> ┌ Error: MethodError: no method matching add_and_display(::JSON3.Array{Int64, Base.CodeUnits{UInt8, String}, SubArray{UInt64, 1, Vector{UInt64}, Tuple{UnitRange{Int64}}, true}})
│
│ Closest candidates are:
│ add_and_display(!Matched::Vector{Int64})
│ @ Main /home/shiva/gitcode/Maths-project/module/Genie/addfun.jl:3
│ add_and_display(!Matched::Vector{<:Union{Float64, Int64}})
│ @ Main /home/shiva/gitcode/Maths-project/module/Genie/addfun.jl:3