Skip to content

Commit 95c7e65

Browse files
committed
9.4 - 9.5
1 parent 5ea18ab commit 95c7e65

5 files changed

Lines changed: 804 additions & 32 deletions

File tree

part9/bmiCalculator.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
interface BmiValues {
2-
value1: number;
3-
value2: number;
4-
}
1+
// interface BmiValues {
2+
// value1: number;
3+
// value2: number;
4+
// }
55

6-
const parseArguments = (args: Array<string>): BmiValues => {
7-
if (args.length < 4) throw new Error("Not enough arguments");
8-
if (args.length > 4) throw new Error("Too many arguments");
6+
// const parseArguments = (args: Array<string>): BmiValues => {
7+
// if (args.length < 4) throw new Error("Not enough arguments");
8+
// if (args.length > 4) throw new Error("Too many arguments");
99

10-
if (!isNaN(Number(args[2])) && !isNaN(Number(args[3]))) {
11-
return {
12-
value1: Number(args[2]),
13-
value2: Number(args[3]),
14-
};
15-
} else {
16-
throw new Error("Provided values were not numbers!");
17-
}
18-
};
10+
// if (!isNaN(Number(args[2])) && !isNaN(Number(args[3]))) {
11+
// return {
12+
// value1: Number(args[2]),
13+
// value2: Number(args[3]),
14+
// };
15+
// } else {
16+
// throw new Error("Provided values were not numbers!");
17+
// }
18+
// };
1919

20-
const calculateBMI = (height: number, weight: number): string => {
20+
export const calculateBMI = (height: number, weight: number): string => {
2121
const bmiValue = (weight / height / height) * 10000;
2222
switch (true) {
2323
case bmiValue < 16.0:
@@ -39,16 +39,16 @@ const calculateBMI = (height: number, weight: number): string => {
3939
}
4040
};
4141

42-
const a: number = Number(process.argv[2]);
43-
const b: number = Number(process.argv[3]);
42+
// const a: number = Number(process.argv[2]);
43+
// const b: number = Number(process.argv[3]);
4444

45-
try {
46-
const { value1, value2 } = parseArguments(process.argv);
47-
console.log(calculateBMI(value1, value2));
48-
} catch (error: unknown) {
49-
let errorMessage = "Something bad happened.";
50-
if (error instanceof Error) {
51-
errorMessage += " Error: " + error.message;
52-
}
53-
console.log(errorMessage);
54-
}
45+
// try {
46+
// const { value1, value2 } = parseArguments(process.argv);
47+
// console.log(calculateBMI(value1, value2));
48+
// } catch (error: unknown) {
49+
// let errorMessage = "Something bad happened.";
50+
// if (error instanceof Error) {
51+
// errorMessage += " Error: " + error.message;
52+
// }
53+
// console.log(errorMessage);
54+
// }

part9/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import express from "express";
2+
import { calculateBMI } from "./bmiCalculator";
3+
4+
const app = express();
5+
6+
app.get("/hello", (_req, res) => {
7+
res.send("Hello Full Stack!");
8+
});
9+
10+
app.get("/bmi", (req, res) => {
11+
const { height, weight } = req.query;
12+
if (!height || !weight) {
13+
res.status(400).send({ error: "malformatted parameters" });
14+
}
15+
const heightNum = Number(height);
16+
const weightNum = Number(weight);
17+
18+
if (isNaN(heightNum) || isNaN(weightNum)) {
19+
res.status(400).send({ error: "malformatted parameters" });
20+
}
21+
22+
const bmi = calculateBMI(heightNum, weightNum);
23+
res.send({
24+
weight: weightNum,
25+
height: heightNum,
26+
bmi,
27+
});
28+
});
29+
30+
app.listen(3333, () => {
31+
console.log("🚀 Server started on port 3333!");
32+
});

part9/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
"license": "MIT",
66
"scripts": {
77
"calculateBMI": "ts-node bmiCalculator.ts",
8-
"calculateExercises": "ts-node exerciseCalculator.ts"
8+
"calculateExercises": "ts-node exerciseCalculator.ts",
9+
"start": "ts-node index.ts",
10+
"dev": "ts-node-dev index.ts"
911
},
1012
"devDependencies": {
13+
"@types/express": "^4.17.13",
1114
"@types/node": "^17.0.10",
1215
"ts-node": "^10.4.0",
16+
"ts-node-dev": "^1.1.8",
1317
"tslib": "^2.3.1",
1418
"typescript": "^4.5.4"
19+
},
20+
"dependencies": {
21+
"express": "^4.17.2"
1522
}
1623
}

part9/tsconfig.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"compilerOptions": {
3-
"noImplicitAny": true
3+
"noImplicitAny": true,
4+
"noImplicitReturns": true,
5+
"strictNullChecks": true,
6+
"strictPropertyInitialization": true,
7+
"strictBindCallApply": true,
8+
"noUnusedLocals": true,
9+
"noUnusedParameters": true,
10+
"noImplicitThis": true,
11+
"alwaysStrict": true,
12+
"esModuleInterop": true,
13+
"declaration": true
414
}
515
}

0 commit comments

Comments
 (0)