So I Have The Code as Follows:
const Solver = new Object
Solver.circle = function(r){
area = Math.PI * r**2
perimeter = Math.PI * 2*r
return "Area:" + area + " Perimeter:" + perimeter
}
Now I Would Like To Get the value of area and perimeter seperately like using Something like this
Solver.circle(2).area \\ 12.57
Solver.circle(2).perimeter \\ 12.57
Solver.circle(2) \\ Area:12.57 Perimeter:12.57
Is there any way i could achieve something like this. If Not What could be the best Alternative Other than having different Main Functions like Solve.circlearea(2) or Solve.circleperi(2)
Sure! Just return an object!
const Solver = new Object
Solver.circle = function(r){
area = Math.PI * r**2
perimeter = Math.PI * 2*r
return {area, perimeter}
}
Note that you can also do return {area: area, perimeter: perimeter}, but it is not necessary and JS will understand you even if you don't do that. So here you are just returning an object and in your usage you are calling the function and accessing the property from the returned object.
If you replace your function with this, your API example will work as is!