-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
331 lines (283 loc) · 9.65 KB
/
Copy pathscript.js
File metadata and controls
331 lines (283 loc) · 9.65 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"use strict";
const account1 = {
owner: "Akhil Kholia",
movements: [200, 455, -306, 25000, -642, -133, 79, 1300],
interestRate: 1.2, // %
pin: 1111,
movementsDates: [
"2020-11-18T21:31:17.178Z",
"2020-12-23T07:42:02.383Z",
"2021-01-28T09:15:04.904Z",
"2021-04-01T10:17:24.185Z",
"2021-05-08T14:11:59.604Z",
"2021-07-26T17:01:17.194Z",
"2021-07-28T23:36:17.929Z",
"2021-08-01T10:51:36.790Z",
],
};
const account2 = {
owner: "Mohit Kang",
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
movementsDates: [
"2020-11-01T13:15:33.035Z",
"2020-11-30T09:48:16.867Z",
"2020-12-25T06:04:23.907Z",
"2021-01-25T14:18:46.235Z",
"2021-02-05T16:33:06.386Z",
"2021-04-10T14:43:26.374Z",
"2021-06-25T18:49:59.371Z",
"2021-07-26T12:01:20.894Z",
],
};
const account3 = {
owner: "Abhishek Thakur",
movements: [500, 3900, -1500, 790, 3330, -1000, 500, -30],
interestRate: 1,
pin: 3333,
movementsDates: [
"2020-11-01T13:15:33.035Z",
"2020-11-30T09:48:16.867Z",
"2020-12-25T06:04:23.907Z",
"2021-01-25T14:18:46.235Z",
"2021-02-05T16:33:06.386Z",
"2021-04-10T14:43:26.374Z",
"2021-06-25T18:49:59.371Z",
"2021-07-26T12:01:20.894Z",
],
};
const accounts = [account1, account2, account3];
// Elements
const labelWelcome = document.querySelector(".welcome");
const labelDate = document.querySelector(".date");
const labelBalance = document.querySelector(".balance__value");
const labelSumIn = document.querySelector(".summary__value--in");
const labelSumOut = document.querySelector(".summary__value--out");
const labelSumInterest = document.querySelector(".summary__value--interest");
const labelTimer = document.querySelector(".timer");
const containerApp = document.querySelector(".app");
const containerMovements = document.querySelector(".movements");
const btnLogin = document.querySelector(".login__btn");
const btnTransfer = document.querySelector(".form__btn--transfer");
const btnLoan = document.querySelector(".form__btn--loan");
const btnClose = document.querySelector(".form__btn--close");
const btnSort = document.querySelector(".btn--sort");
const inputLoginUsername = document.querySelector(".login__input--user");
const inputLoginPin = document.querySelector(".login__input--pin");
const inputTransferTo = document.querySelector(".form__input--to");
const inputTransferAmount = document.querySelector(".form__input--amount");
const inputLoanAmount = document.querySelector(".form__input--loan-amount");
const inputCloseUsername = document.querySelector(".form__input--user");
const inputClosePin = document.querySelector(".form__input--pin");
const formatMovementDate=function(date){
const calculateDaysPassed=(date1,date2)=>Math.round(Math.abs(date1-date2)/(1000*60*60*24));
const daysPassed=calculateDaysPassed(new Date(),date);
if(daysPassed===0)return 'Today';
if(daysPassed===1)return 'Yesterday';
if(daysPassed<=7)return `${daysPassed} days ago`;
else{
const day = `${date.getDate()}`.padStart(2, 0);
const month = `${date.getMonth() + 1}`.padStart(2, 0);
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
};
const displayMovements = function (acc, sort = false) {
containerMovements.innerHTML = "";
const movs = sort
? acc.movements.slice().sort((a, b) => a - b)
: acc.movements;
movs.forEach(function (mov, i) {
const type = mov > 0 ? "deposit" : "withdrawal";
const date = new Date(acc.movementsDates[i]);
const displayDate=formatMovementDate(date);
const html = `<div class="movements__row">
<div class="movements__type movements__type--${type}">${type} </div>
<div class="movements__date">${displayDate}</div>
<div class="movements__value">₹${mov.toFixed(2)}</div>
</div>`;
containerMovements.insertAdjacentHTML("afterbegin", html);
});
};
const createUsernames = function (accs) {
accs.forEach(function (acc) {
acc.userName = acc.owner
.toLowerCase()
.split(" ")
.map(function (element) {
// return element.slice(0,1);
return element[0];
})
.join("");
});
};
createUsernames(accounts);
console.log(accounts);
const displayTotalBalance = function (acct) {
const totalBalance = acct.movements.reduce(function (acc, mov) {
return acc + mov;
}, 0);
acct.tbalance = totalBalance;
labelBalance.textContent = `₹${totalBalance.toFixed(2)}`;
};
const calcDisplaySummary = function (acct) {
const incomes = acct.movements
.filter((mov) => mov > 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumIn.textContent = `₹${incomes.toFixed(2)}`;
const out = Math.abs(
acct.movements.filter((mov) => mov < 0).reduce((acc, mov) => acc + mov, 0)
);
labelSumOut.textContent = `₹${out.toFixed(2)}`;
const interest = acct.movements
.filter((mov) => mov > 0)
.map((deposit) => (deposit * acct.interestRate) / 100)
.filter((int) => int >= 1)
.reduce((acc, int) => acc + int, 0);
labelSumInterest.textContent = `₹${+interest.toFixed(2)}`;
};
// calcDisplaySummary(account1.movements);
// const accountt=accounts.find(acc=>acc.owner==='Akhil Kholia');
// console.log(accountt);
const updateUI = function (acc) {
//Display movements
displayMovements(acc);
//display balance
// displayTotalBalance(currentAccount.movements);
displayTotalBalance(acc);
//display summary
calcDisplaySummary(acc);
};
let currentAccount,timer;
btnLogin.addEventListener("click", function (e) {
e.preventDefault();
currentAccount = accounts.find(
(acc) => acc.userName === inputLoginUsername.value
);
console.log(currentAccount);
if (currentAccount?.pin === Number(inputLoginPin.value)) {
//display UI and message
labelWelcome.textContent = `Welcome back, ${
currentAccount.owner.split(" ")[0]
}`;
containerApp.style.opacity = 100;
const now = new Date();
const day = `${now.getDate()}`.padStart(2, 0);
const month = `${now.getMonth() + 1}`.padStart(2, 0);
const year = now.getFullYear();
const hours = `${now.getHours()}`.padStart(2, 0);
const min = `${now.getMinutes()}`.padStart(2, 0);
labelDate.textContent = `${day}/${month}/${year} ${hours}:${min}`;
if(timer) clearInterval(timer);
timer=startLogOutTimer();
updateUI(currentAccount);
}
//clear the input fields
inputLoginUsername.value = "";
inputLoginPin.value = "";
inputLoginPin.blur();
});
btnTransfer.addEventListener("click", function (e) {
e.preventDefault();
//find the taccount and ensure value is more than total amount in current
const amount = Number(inputTransferAmount.value);
const tacct = accounts.find((acc) => acc.userName === inputTransferTo.value);
if (tacct === undefined) alert("Sorry!! No such account exist.");
else {
if (
amount > 0 &&
amount <= currentAccount.tbalance &&
tacct.userName !== currentAccount.userName
) {
//add as deposit into the taccount
tacct.movements.push(amount);
//deduct amount from current as withrawal
currentAccount.movements.push(0 - amount);
console.log(tacct);
console.log(currentAccount);
//update the ui of current
// displayMovements(currentAccount.movements);
// displayTotalBalance(currentAccount);
// calcDisplaySummary(currentAccount);
currentAccount.movementsDates.push(new Date().toISOString());
tacct.movementsDates.push(new Date().toISOString());
updateUI(currentAccount);
} else {
alert("Sorry,Transaction Failed!! ");
}
}
inputTransferTo.value = "";
inputTransferAmount.value = "";
inputTransferAmount.blur();
clearInterval(timer);
timer=startLogOutTimer();
});
btnClose.addEventListener("click", function (e) {
e.preventDefault();
// confirm username and pin
// const ind =accounts.findIndex(acc=> acc.userName===inputCloseUsername.value && acc.pin===Number(inputClosePin.value));
// console.log(ind);
// accounts.splice(ind,1);
if (
inputCloseUsername.value === currentAccount.userName &&
Number(inputClosePin.value) === currentAccount.pin
) {
const ind = accounts.findIndex((acc) => acc === currentAccount);
console.log(ind);
accounts.splice(ind, 1);
containerApp.style.opacity = 0;
}
inputCloseUsername.value = "";
inputClosePin.value = "";
inputClosePin.blur();
});
btnLoan.addEventListener("click", function (e) {
e.preventDefault();
const amount = Math.floor(inputLoanAmount.value);
if (
amount > 0 &&
currentAccount.movements.some((mov) => mov >= amount * 0.1)
) {
setTimeout( function(){currentAccount.movements.push(amount);
currentAccount.movementsDates.push(new Date().toISOString());
updateUI(currentAccount);
},3000);
}
else {
alert("Sorry!! Loan cannot be processed");
}
inputLoanAmount.value = "";
inputLoanAmount.blur();
clearInterval(timer);
timer= startLogOutTimer();
});
let toggleSort = false;
btnSort.addEventListener("click", function (e) {
e.preventDefault();
displayMovements(currentAccount, !toggleSort);
toggleSort = !toggleSort;
btnSort.blur();
});
const startLogOutTimer=function(){
//call for 5 min timer
let time=300;
const tick=function(){
const min=String(Math.trunc(time/60)).padStart(2,0);
const sec=String((time%60)).padStart(2,0);
labelTimer.textContent=`${min}:${sec}`;
if(time===0){
clearInterval(timer);
labelWelcome.textContent='Log in to get started';
containerApp.style.opacity = 0;
}
time--;
}
//call the timer every seconds
//in each call.print remaining time in ui
//log out after 5 min
tick();
const timer=setInterval(tick,1000);
return timer;
}
/////////////////////////////////////////////////