How can mixed number JSON inputs be converted to numbers using split() and reduce() in Node JS?

Viewed 28

I am building a picture framing REST API calculator using Node JS. The code I've written for it is this:

app.post("/api/calculator/singlematapi", (req,res) => {
    
    let FrameWidth = 1 * req.body.FrameWidth.split("/").reduce((a, b) => a / b);
    let FrameWidthFraction = 1 * req.body.FrameWidthFraction.split("/").reduce((a, b) => a / b);
    let FrameHeight = 1 * req.body.FrameHeight.split("/").reduce((a, b) => a / b);
    let FrameHeightFraction = 1 * req.body.FrameHeightFraction.split("/").reduce((a, b) => a / b);
    let PictureWidth = 1 * req.body.PictureWidth.split("/").reduce((a, b) => a / b);
    let PictureWidthFraction = 1 * req.body.PictureWidthFraction.split("/").reduce((a, b) => a / b);
    let PictureHeight = 1 * req.body.PictureHeight.split("/").reduce((a, b) => a / b);
    let PictureHeightFraction = 1 * req.body.PictureHeightFraction.split("/").reduce((a, b) => a / b);
    let MatOverlap = 1 * req.body.MatOverlap.split("/").reduce((a, b) => a / b);
    let MatOverlapFraction = 1 * req.body.MatOverlapFraction.split("/").reduce((a, b) => a / b);
    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(height).toString()}", Height Cut = ${new Fraction(width).toString()}"`);
});

Therefore, the input JSON POST request would be, for example:

{
    "FrameWidth": "16",
    "FrameWidthFraction": "1/2",
    "FrameHeight": "20",
    "FrameHeightFraction": "0",
    "PictureWidth": "11",
    "PictureWidthFraction": "0",
    "PictureHeight": "17",
    "PictureHeightFraction": "0",
    "MatOverlap": "0",
    "MatOverlapFraction": "1/2"
}

The response would be: Width Cut = 3", Height Cut = 1 3/4"


The problem I'm running into is that I can't seem to figure out how to allow for mixed numbers in my key/value pairs of my object.

I currently have to have a separate key/value pair for the integer, and a separate pair for the fraction as shown above, for example:

{
    "FrameWidth": "16",
    "FrameWidthFraction": "1/2"
}

My goal is to reduce the number of key/value pairs by allowing mixed numbers as an input like this:

{
    "FrameWidth": "16 1/2"
}

The piece of code in question:

let FrameWidth = 1 * req.body.FrameWidth.split("/").reduce((a, b) => a / b);
let FrameWidthFraction = 1 * req.body.FrameWidthFraction.split("/").reduce((a, b) => a / b);

...only allows for individual integers or individual fractions, but not a combined mixed number.

Can mixed number inputs in JSON be converted to numbers using split() and reduce() in Node JS with the code I'm using?

1 Answers

This problem has been resolved by using the correct split() and reduce() methods.

Reading examples from here: JavaScript reduce() Method and from here: JavaScript split() Method

I was able to fix my issue by finding:

let FrameWidth = 1 * req.body.FrameWidth.split("/").reduce((a, b) => a / b);

And changing it to:

let FrameWidth = 1 * req.body.FrameWidth.split("/").reduce((a, denom) => a.split(" ").reduce((int, numer) => 1 * numer + int * denom) / denom);

Therefore, the input JSON POST request would become:

{
    "FrameWidth": "16",
    "FrameHeight": "20",
    "PictureWidth": "11",
    "PictureHeight": "17",
    "MatOverlap": "1 1/2"
}
Related