-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.ts
62 lines (62 loc) · 2.25 KB
/
Calculator.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env node
import inquirer from "inquirer";
import { add } from "./Addition.js";
import { sub } from "./Subtraction.js";
import { mul } from "./Multiplication.js";
import { div } from "./Division.js";
const Calculator = () => {
const Question = () => {
inquirer.prompt([
{
type: "list",
name: "operator",
message: "Choose Your Desired Operator",
choices: ["Addition", "Subtraction", "Multiplication", "Division"]
},
{
type: "number",
name: "num1",
message: "Enter your First Number "
},
{
type: "number",
name: "num2",
message: "Enter your Second Number "
},
])
.then((answers) => {
if (answers.operator === "Addition") {
const addresult = add(answers.num1, answers.num2);
console.log(`${answers.num1} + ${answers.num2} = ${addresult}`);
}
else if (answers.operator === "Subtraction") {
const subresult = sub(answers.num1, answers.num2);
console.log(`${answers.num1} - ${answers.num2} = ${subresult}`);
}
else if (answers.operator === "Multiplication") {
const mulresult = mul(answers.num1, answers.num2);
console.log(`${answers.num1} * ${answers.num2} = ${mulresult}`);
}
else if (answers.operator === "Division") {
const divresult = div(answers.num1, answers.num2);
console.log(`${answers.num1} / ${answers.num2} = ${divresult}`);
}
inquirer.prompt([
{
type: "confirm",
name: "continue",
message: "Do you want to perform another calculation?"
}
]).then((answer) => {
if (answer.continue) {
Question(); // Start the calculator again
}
else {
console.log("Thank you for using the calculator. Goodbye!");
}
});
});
};
Question();
};
Calculator();