Hello, following this great post and substituting the C# code with the below (no changes required to the JSON code) I have created a function that returns the factorial of any integer between 0 and 170 or returns -1 if the input is not an integer or out of the range.
The problem is that the number is returned as a string. Could anyone suggest any changes to the C# and JSON code to make this happen? I have tried to change the "string" to "number" and "double" in both the C# and JSON but it has not worked.
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
name = name ?? data?.name;
// Initialise the return variable
double factorial = -1;
// Convert the input to a short integer and calculate factorial. Result is -1 if input is not an integer between 0 and 170
ushort n;
if (ushort.TryParse(name, out n))
{
if (n <= 170)
// If input is >170 then it will be out of range for double type variable
{
if (n == 0)
{
factorial = 1;
// Factorial of 0 is 1
}
else
{
int numberInt = n;
factorial = n;
for (int i = 1; i < n; i++)
{
factorial = factorial * i;
}
}
}
}
return req.CreateResponse(HttpStatusCode.OK, factorial.ToString());
// return the result as a string since I don't know how to return a number to PowerApps
}