-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
41 lines (32 loc) · 1.21 KB
/
Copy pathcli.js
File metadata and controls
41 lines (32 loc) · 1.21 KB
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
38
39
40
41
// this is the cli file which the user will interact with
import readline from 'readline';
import { calculateBMI } from './bmi.js';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function prompt(question) {
return new Promise((resolve) => rl.question(question, resolve));
}
async function main() {
console.log("--- BMI Calculator ---");
try {
const feet = parseFloat(await prompt("Enter height (feet): "));
const inches = parseFloat(await prompt("Enter height (inches): "));
const weight = parseFloat(await prompt("Enter weight (pounds): "));
// simple error handling
if (isNaN(feet) || isNaN(inches) || isNaN(weight) || feet < 0 || inches < 0 || weight <= 0) {
console.error("\nError: Please enter valid positive numbers. Weight must be greater than 0.");
rl.close();
return;
}
const result = calculateBMI(feet, inches, weight);
console.log(`\nYour BMI: ${result.bmi}`);
console.log(`Category: ${result.category}`);
} catch (error) {
console.error("Invalid input. Please enter numeric values.");
} finally {
rl.close();
}
}
main();