-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaurav_recursionTask.html
275 lines (237 loc) · 6.03 KB
/
saurav_recursionTask.html
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<script>
var userData = [];
var numReg = /^\d+$/;
var id = 101;
var adminList = [
{ id: 1, name: "saurav", password: 123456, status: 1 },
{ id: 2, name: "keshav", password: 123456, status: 1 },
];
function checklen(inp, len) {
//-------------------------------Check Length--------------------------------------
var inputString = prompt(inp);
if (inputString.length < len) {
inputString = checklen(inp, len);
}
return inputString;
}
function checkNum(inp) {
//-------------------------------Check Number--------------------------------------
var num = prompt(inp);
if (!num.match(numReg)) {
num = checkNum(inp);
}
return parseInt(num);
}
function userOperation(userObject) {
//-------------------------------User Operation--------------------------------------
var userChoice = checkNum(
"What's your user role ? \n1. WithDraw \n2. Deposit \n3. Show Balance \n4. Exit"
);
switch (userChoice) {
case 1:
//-------------------------WithDraw------------------------------
var withDrawAmount = checkNum("Enter WithDraw Amount: ");
if (userObject.accountBalance < withDrawAmount) {
alert("Insufficient Balance!");
} else {
userObject.accountBalance -= withDrawAmount;
alert("WithDrawed Successfully, Amount has been debited!");
}
break;
case 2:
//---------------------------Deposit------------------------------
var depositAmount = checkNum("Enter Deposit Amount: ");
userObject.accountBalance += depositAmount;
alert("Deposited Successfully, Amount has been credited!");
break;
case 3:
//-------------------------Show Balance------------------------------
alert(
"Account Number= " +
userObject.accountNumber +
"\nAccount Balance= " +
userObject.accountBalance
);
break;
case 4:
alert("Thank You!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
userOperation(userObject);
}
function user() {
//--------------------------------User---------------------------------------
var flag = login(userData);
if (flag == 0) {
alert("Invalid User Name !!!");
} else if (flag == 1) {
alert("Wrong Password !!!");
} else {
userOperation(flag);
}
}
function viewData() {
//-------------------------------View--------------------------------------
var outputString = "";
for (var v of userData) {
outputString +=
"User Id: " +
v.id +
" - " +
"User Name: " +
v.name +
" - " +
"Full Name: " +
v.fullName +
"\n.Account Number: " +
v.accountNumber +
" - " +
"Account Balance: " +
v.accountBalance +
" - " +
"Status: " +
v.status +
"\n";
}
alert(outputString);
}
function manageUser() {
//-----------------------Manage User---------------------
viewData();
var flag = -1;
var approvalId = checkNum(
"Which user you want to approve ? \n(Please enter the User Id): "
);
for (var i of userData) {
if (approvalId == i.id && i.status == 0) {
flag = 0;
i.status = 1;
var result = "";
for (var j = 0; j < 16; j++) {
result += Math.floor(Math.random() * 10);
}
i.accountNumber = result;
alert("User Approved. Thank You!");
break;
} else if (approvalId == i.id && i.status == 1) {
flag = 1;
alert("User has been already Approved. Thank You!");
}
}
if (flag == -1) {
alert("Invalid User Id, Please enter a valid User Id!!");
}
}
function adminOperation() {
var adminChoice = checkNum(
"What's your user role ? \n1. Manage User \n2. Exit"
);
switch (adminChoice) {
case 1:
manageUser();
break;
case 2:
alert("Thank you!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
adminOperation();
}
// ---------------------------------------Just for practice-------------------------
function abc(pp, i, ap) {
var ram = 0;
if (i < pp.length - 1) {
if (ap == pp[i].name) {
ram = 1;
}
i++;
ram = abc(pp, i, ap);
}
return ram;
}
var ss = abc(list, 0, ko);
// -------------------------------------Just for practice----------------------------
function login(list) {
var loginFlag = 0;
var loginName = checklen("Enter your Name: ", 3);
// ----------------yaha per for loop ko recursion meh kese convert kere...?--------------
for (var l of list) {
if (loginName == l.name) {
loginFlag = 1;
var loginPassword = checklen("Enter your Password: ", 6);
if (loginPassword == l.password) {
if (l.status == 1) {
loginFlag = l;
alert("Login Successfull, Welcome :)");
} else {
alert("You're not been approved yet!!");
}
}
}
}
return loginFlag;
}
function admin() {
//---------------------------Admin---------------------------------------
var adminFlag = login(adminList);
if (adminFlag == 0) {
alert("Invalid Admin Name !!!");
} else if (adminFlag == 1) {
alert("Invalid Admin Password !!!");
} else {
adminOperation();
}
}
function visitor() {
//------------------------------Visitor--------------------------------------
var name = checklen("Enter the username: \nEnter atleast 3 chars", 3);
var password = checklen("Enter the password: \nEnter atleast 6 chars", 6);
var fullName = checklen("Enter the full name: \nEnter atleast 5 chars", 5);
var status = 0;
var accountNumber = "0";
var accountBalance = 0;
var dummy = {
id: id++,
name,
password,
fullName,
accountNumber,
accountBalance,
status,
};
userData.push(dummy);
alert("User data added successfully !!!");
}
function main() {
var choice = checkNum(
"What's your user role ? \n1. Visitor \n2. Admin \n3. User \n4. Exit"
);
switch (choice) {
case 1:
visitor();
break;
case 2:
admin();
break;
case 3:
user();
break;
case 4:
alert("Thank you! for visiting our system...");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
main();
}
main();
</script>