-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
107 lines (88 loc) · 1.87 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
var response = query();
while(!isQuit(response)){
if(response === 'a' || response === 's' || response === 'm' || response === 'd' || response === 'e'){
var x = getInput();
var y = getInput();
var out, operation;
switch(response){
case 'a':
out = add(x, y);
operation = 'sum';
break;
case 's':
out = sub(x, y);
operation = 'difference';
break;
case 'm':
out = mul(x, y);
operation = 'product';
break;
case 'd':
out = div(x, y);
operation = 'division';
break;
case 'e':
out = exp(x, y);
operation = 'power';
}
var string = getString(x, y, out, operation);
} else {
var x = getInput();
var out, operation;
switch(response){
case 'r':
out = root(x);
operation = 'square root';
break;
case 'f':
out = fact(x);
operation = 'factorial';
}
var string = getString(x, null, out, operation);
}
console.log(string);
response = query();
}
function query(){
var response = prompt('(a)dd, (s)ub, (m)ul, (d)iv, (e)xp, (r)oot, (f)act, (q)uit');
return response.toLowerCase();
}
function isQuit(letter){
return letter === 'q';
}
function getInput(){
var value = prompt('Enter value');
return value * 1;
}
function add(x, y){
return x + y;
}
function sub(x, y){
return x - y;
}
function mul(x, y){
return x * y;
}
function div(x, y){
return x / y;
}
function exp(x, y){
return Math.pow(x, y);
}
function root(x){
return Math.sqrt(x);
}
function fact(x){
var total = 1;
for(var i = 1; i <= x; i++){
total *= i;
}
return total;
}
function getString(x, y, out, operation){
if(y){
return 'The ' + operation + ' of ' + x + ' and ' + y + ' is ' + out;
} else {
return 'The ' + operation + ' of ' + x + ' is ' + out;
}
}