diff --git a/Part_9/scripts/bmiCalculatorExpress.ts b/Part_9/scripts/bmiCalculatorExpress.ts new file mode 100644 index 0000000..b37b6b8 --- /dev/null +++ b/Part_9/scripts/bmiCalculatorExpress.ts @@ -0,0 +1,29 @@ +interface bmiOutput { + bmi: number, + bmiRemark: string +} + +export const calculateBmi = (height: number, weight: number): bmiOutput => { + const bmi: number = (weight / (height * height)) * 10000; + let bmiRemark; + + if (bmi < 16) { + bmiRemark = 'You are Severely Under-Weight 💀'; + } else if (16 < bmi && bmi < 18.4) { + bmiRemark = 'You are Under-Weight 😥'; + } else if (18.5 < bmi && bmi < 24.9) { + bmiRemark = 'You are Healthy 😁'; + } else if (25 < bmi && bmi < 29.9) { + bmiRemark = 'You are Over-Weight 😳'; + } else if (30 < bmi && bmi < 34.9) { + bmiRemark = 'You are Moderately Obese 😰'; + } else if (35 < bmi && bmi < 39.9) { + bmiRemark = 'You are Severely Obese 😨'; + } else if (bmi > 40) { + bmiRemark = 'You are Morbidly Obese 💀'; + } else { + throw new Error('Error occured while calculating BMI'); + } + + return { bmi: Number(bmi.toFixed(2)), bmiRemark } +} diff --git a/Part_9/scripts/index.ts b/Part_9/scripts/index.ts index d1b2578..a7cd014 100644 --- a/Part_9/scripts/index.ts +++ b/Part_9/scripts/index.ts @@ -1,12 +1,33 @@ import express from 'express'; -const app = express() +const app = express(); + +import { calculateBmi } from './bmiCalculatorExpress'; + +app.use(express.json()); app.get('/hello', (_req, res) => { - res.send('Hello Full Stack!') + res.send('Hello Full Stack!'); +}) + +app.get ('/bmi', (req, res) => { + const { height, weight } = req.query + + if (!height || !weight) { + return res.status(400).send({ + error: 'Malformatted or Missing Parameters' + }) + } + + try { + const {bmi, bmiRemark } = calculateBmi(Number(height), Number(weight)) + return res.status(200).json({ height, weight, bmi, bmiRemark}) + } catch (error) { + return res.status(400).send({ error: error.message }) + } }) const PORT = 3003 app.listen(PORT, () => { - console.log(`Server running at Port ${PORT}`) + console.log(`Server running at Port ${PORT}`); })