-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmiCalculator.ts
37 lines (31 loc) · 924 Bytes
/
bmiCalculator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const calculateBmi = (height: number, weight: number): string => {
const bmi = weight / Math.pow(height / 100, 2);
if (bmi < 18.5) {
return "Underweight";
} else if (bmi < 25) {
return "Normal range";
} else if (bmi < 30) {
return "Overweight";
} else {
return "Obese";
}
};
if (require.main === module) {
// isolate the real input args from extra info
const args = process.argv.slice(2);
//input validation
if (args.length !== 2) {
console.log(
"The function takes two positive numbers as args: height in cm and weight in kg."
);
process.exit(1);
}
const height = Number(args[0]);
const weight = Number(args[1]);
if (isNaN(height) || isNaN(weight) || height <= 0 || weight <= 0) {
console.log("One (or both) of the arguments is not a positive number.");
process.exit(1);
}
console.log(calculateBmi(height, weight));
}
export { calculateBmi };