I am in the process of building a picture framing calculator API using Node JS & Express. The JavaScript I am using is:
app.post("/api/chatfuel/calculator/triplematapi", (req,res) => {
if (!(req.body.FrameWidth > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let FrameWidth = 1 * req.body.FrameWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.FrameHeight > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let FrameHeight = 1 * req.body.FrameHeight.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.PictureWidth > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let PictureWidth = 1 * req.body.PictureWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.FrameHeight > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let PictureHeight = 1 * req.body.PictureHeight.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.MiddleMat > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let MiddleMat = 1 * req.body.MiddleMat.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
if (!(req.body.BottomMat > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
let BottomMat = 1 * req.body.BottomMat.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);
let TopMatWidth = ((FrameHeight)-(PictureHeight))/2-(MiddleMat);
let TopMatHeight = ((FrameWidth)-(PictureWidth))/2-(MiddleMat);
let MiddleMatWidth = ((FrameHeight)-(PictureHeight))/2;
let MiddleMatHeight = ((FrameWidth)-(PictureWidth))/2;
let BottomMatWidth = ((FrameHeight)-(PictureHeight))/(2)+(BottomMat);
let BottomMatHeight = ((FrameWidth)-(PictureWidth))/(2)+(BottomMat);
res.json({"messages": [{"text": "Place the parallel mat guide at the following inch mark, then make the respective width and height cut starting with the Top Mat, Middle Mat, then Bottom Mat:"},{"text": `Top Mat Width Cut = ${new Fraction(TopMatWidth).toString()} inches`},{"text": `Top Mat Height Cut = ${new Fraction(TopMatHeight).toString()} inches`},{"text": `Middle Mat Width Cut = ${new Fraction(MiddleMatWidth).toString()} inches`},{"text": `Middle Mat Height Cut = ${new Fraction(MiddleMatHeight).toString()} inches`},{"text": `Bottom Mat Width Cut = ${new Fraction(BottomMatWidth).toString()} inches`},{"text": `Bottom Mat Height Cut = ${new Fraction(BottomMatHeight).toString()} inches`},{"buttons": [{"title": "Go to I Was Framed!","type": "web_url","url": "https://iwasframed.com/"}]}]});
});
Therefore, an example POST request would be:
{
"FrameWidth": "16",
"FrameHeight": "20",
"PictureWidth": "11",
"PictureHeight": "17",
"MiddleMat": "1",
"BottomMat": "1/2"
}
The problem I'm running into is that before I added a check to verify that the input value is positive, then the return response would make the calculation. However, by adding this code (I had to use -1
because 0
is a valid, positive input):
if (!(req.body.BottomMat > -1)) {
res.status(422).json({ messages: "Please insert a positive value" });
}
Then any fraction
I input such as 1/2
- it throws the error:
{
"messages": "Please insert a positive value"
}
It should not be throwing a error message because these fractions are positive inputs. Can I get some guidance on how to prevent an error message when the input is a positive fraction as well?