-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (52 loc) · 1.5 KB
/
index.js
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
"use strict";
exports.__esModule = true;
console.log('CLI CALCULATOR :-\n');
var readline_sync_1 = require("readline-sync");
function main() {
var firstStr = (0, readline_sync_1.question)('Enter First number: ');
var operator = (0, readline_sync_1.question)('Enter Operator: ');
var secondStr = (0, readline_sync_1.question)('Enter Second number: ');
var validInput = isNumber(firstStr) && isNumber(secondStr) && isOperator(operator);
if (validInput) {
var firstNum = parseInt(firstStr);
var secondNum = parseInt(secondStr);
var result = calculate(firstNum, operator, secondNum);
console.log("Result: ".concat(result));
}
else {
console.error('\n**********Invalid Input**********\n');
main();
}
}
function calculate(firstNum, operator, secondNum) {
switch (operator) {
case '+':
return firstNum + secondNum;
case '-':
return firstNum - secondNum;
case '*':
return firstNum * secondNum;
case '/':
return firstNum / secondNum;
default:
return 0;
}
}
function isOperator(operator) {
switch (operator) {
case '+':
case '-':
case '*':
case '/':
return true;
default:
return false;
}
}
// Convert string to number so we can use it in the calculation without geeting NAN error
function isNumber(str) {
var num = parseInt(str);
var isNum = !isNaN(num);
return isNum;
}
main();