I am creating an API for a picture framing calculator. I am using Node JS with Express. The code that I’m using is this:
app.post("/api/calculator/singlematapi", (req,res) => { let FrameWidth = req.body.FrameWidth; let FrameWidthFraction = req.body.FrameWidthFraction let FrameHeight = req.body.FrameHeight; let FrameHeightFraction = req.body.FrameHeightFraction; let PictureWidth = req.body.PictureWidth; let PictureWidthFraction = req.body.PictureWidthFraction; let PictureHeight = req.body.PictureHeight; let PictureHeightFraction = req.body.PictureHeightFraction; let MatOverlap = req.body.MatOverlap; let MatOverlapFraction = req.body.MatOverlapFraction let width = (1/2)*((FrameHeight+FrameHeightFraction)-(PictureHeight+PictureHeightFraction)+(MatOverlap+MatOverlapFraction)); let height = (1/2)*((FrameWidth+FrameWidthFraction)-(PictureWidth+PictureWidthFraction)+(MatOverlap+MatOverlapFraction)); res.send(`Width Cut = ${new Fraction(width).toString()}", Height Cut = ${new Fraction(height).toString()}"`); });
Therefore, a JSON POST request would be:
{ "FrameWidth": 16, "FrameWidthFraction": 0, "FrameHeight": 20, "FrameHeightFraction": 0, "PictureWidth": 11, "PictureWidthFraction": 0, "PictureHeight": 17, "PictureHeightFraction": 0, "MatOverlap": 0.5 }
What I am trying to accomplish is that instead of a decimal – a fraction such as 1/2
can be inputted instead. For example:
{ "FrameWidth": 16, "FrameWidthFraction": 0, "FrameHeight": 20, "FrameHeightFraction": 0, "PictureWidth": 11, "PictureWidthFraction": 0, "PictureHeight": 17, "PictureHeightFraction": 0, "MatOverlap": 1/2 }
The problem I’m running into is that although I am able to convert the output
from decimal to fraction using a library and this piece of code:
res.send(`Width Cut = ${new Fraction(width).toString()}", Height Cut = ${new Fraction(height).toString()}"`);
…I am not able to use a fraction
as an input
instead of a decimal.
Unless I’m misunderstanding – it states here: JSON data types – that Fractions
pertaining to Numbers
can only be displayed in decimal type formatting such as 0.5
like I have above under MatOverlap
.
However, according to the same page it states that a forward slash
can be used in a string
.
Can I use the forward slash
as the solidus
to indicate a fraction when JSON data is inputted?
When I attempted to use a string
by changing the above to:
{ "MatOverlap": "1/2" }
…then it throws NaN
error.
EDIT 8/1/2022
This is for further clarification regarding one of the solutions to this question: this solution found below by some random nerd.
Particularly this comment:
You can enter just a 0 with this method as well
I wrote a piece of code to serve as an example of what I mean:
app.post("/add", (req, res)=>{ let n1 = req.body.n1; let n2 = req.body.n2.split("/").reduce((a, b) => a / b); let sum = n1 + n2; res.send(`Sum = ${sum}`); });
If I input:
{ "n1": 1, "n2": "0/1" }
The response is: Sum = 1
{ "n1": 1, "n2": "0" }
The response is: Sum = 10
See how 0
needs to be written as 0/1
in order for the math to work correctly? When the input is 0
I’m looking to write just a 0
.
Advertisement
Answer
you can convert the fraction input to decimal.
let MatOverlap = req.body.MatOverlap.split("/").reduce((a, b) => a / b)
To also accept mixed fractions:
let MatOverlap = req.body.MatOverlap.split("/").reduce( (a, denom) => a.split(" ").reduce((int, numer) => +numer + int * denom) / denom );