-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (49 loc) · 1.35 KB
/
index.js
File metadata and controls
49 lines (49 loc) · 1.35 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
42
43
44
45
46
47
48
49
"use strict";
exports.__esModule = true;
var readline_sync_1 = require("readline-sync");
function main() {
var firstStr = (0, readline_sync_1.question)('Enter first number:\n');
var operator = (0, readline_sync_1.question)('Enter Operator:\n');
var secondStr = (0, readline_sync_1.question)('Enter second number\n');
var validInput = isNumber(firstStr) && isOperator(operator) && isNumber(secondStr);
if (validInput) {
// console.log('is valid.')
var firstNum = parseInt(firstStr);
var secondNum = parseInt(secondStr);
var result = calculate(firstNum, operator, secondNum);
console.log(result);
}
else {
console.log('\ninvalid 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;
}
}
function isOperator(operator) {
switch (operator) {
case '+':
case '-':
case '*':
case '/':
return true;
default:
return false;
}
}
function isNumber(str) {
var maybeNum = parseInt(str);
var isNum = !isNaN(maybeNum);
return isNum;
}
main();