-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (90 loc) · 3.31 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// NAME: OMAR USMAN
// TASK FOR: ENCIRCLE
// LANGUAGE USED: JAVASCRIPT
// PLEASE CHECK readme.txt FOR MORE INFORMATION
const prompt = require("prompt-sync")();
const add = (a , b) =>{ // Sum function to add two values
return a+b;
}
const multiply = (a , b) =>{ // Multiply Function
return a*b;
}
const main = () => { // Main or starting point of Program.. Includes Menu
console.log('===== MAIN MENU =====');
console.log('Press enter -1 to exit');
console.log();
let value = ''; // Get value from user
do {
value = prompt('Please Enter the Command ->');
if(!isNaN(value)){
if(value < 0) {
console.log('Please Enter positive number or Proper Expression...') // Error check for negative num
}
else {
console.log('Result:', value); // Display normal number
}
}
else if(value.split('(').length-1 != value.split(')').length-1){ //ERROR CHECK
console.log('Please Balance the Parenthesis or Check your Command')
}
else if(!value.includes('add') && !value.includes('multiply')){
console.log('Please Enter Valid Command...');
}
else {
const val = value.split(' '); //Check if root is multiply or sum
if(val[0].includes('add')){
businessLogic('add', val);
}
else if(val[0].includes('(multiply')){
businessLogic('multiply', val);
}}
} while (value != '-1');
}
const businessLogic = (type, val) => { // All business logic, includes conditions
let text = '';
text = text.concat(type);
for (let i=1; i< val.length; i++){
if(val[i] === '(add'){
if(text.slice(-1) === ')'){
text = text.concat(',' + val[i].slice(1) + '(')
}
else{
text= text.concat(val[i])
}
}
else if(val[i] === '(multiply'){
if(text.slice(-1) === ')'){
text = text.concat(',' + val[i].slice(1) + '(')
}
else{
text= text.concat('(multiply');
}
}
else if(!(val[i].includes('(')) && !(val[i].includes(')')) && !(val[i].includes('add') || val[i].includes('multiply'))){
if(val[i-1].includes('multiply') || val[i-1].includes('add')){
text = text.concat('(' + val[i] + ',');
}
else if(text.slice(-1) === '('){
text = text.concat(val[i] + ',');
}
}
else if(val[i].includes('(') || val[i].includes(')') && !(val[i].includes('add') || val[i].includes('multiply'))){
if(text.slice(-1) === ','){
text = text.concat(val[i]);
}
else if(text.slice(-1) === ')'){
text = text.concat(',' + val[i]);
}
}
}
if(text.split('(').length-1 != text.split(')').length-1){
text = text.concat(')');
}
if(!text){
console.log('Ooops! something went wrong...') //Error Check
}
else{
console.log('Result',eval(text)); //Display Result
}
}
main(); // Call of Main Function