-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtsCal.ts
35 lines (31 loc) · 1.15 KB
/
tsCal.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
import { question } from "readline-sync";
function myCalculator(): string {
const firstStr: string= question('enter first number\n');
const operator: string= question('enter operator\n');
const secondStr: string= question('enter second number\n');
const isValidExpression=(isNum(firstStr) && isOperator(operator) && isNum(secondStr));
if(isValidExpression){
const firstNum= parseInt(firstStr);
const secondNum= parseInt(secondStr);
switch(operator){
case "+":return `The sum is ${firstNum + secondNum}`;
case "-":return `The subtraction is ${firstNum - secondNum}`;
case "*":return `The product is ${firstNum * secondNum}`;
case "/":return `The division is ${firstNum / secondNum}`;
}
}
return "<<<<___Invalid expression___>>>>";
}
function isNum(num: string):boolean{
return !isNaN(parseInt(num));
}
function isOperator(operator: string):boolean{
switch(operator){
case "+":return true;
case "-":return true;
case "*":return true;
case "/":return true;
default: return false;
}
}
console.log(myCalculator());