-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
633 lines (543 loc) · 18.3 KB
/
script.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
'use strict';
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// BANKIST APP
// Data
const account1 = {
owner: 'Jonas Schmedtmann',
movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
interestRate: 1.2, // %
pin: 1111,
movementsDates: [
'2019-11-18T21:31:17.178Z',
'2019-12-23T07:42:02.383Z',
'2020-01-28T09:15:04.904Z',
'2020-04-01T10:17:24.185Z',
'2020-05-08T14:11:59.604Z',
'2020-10-20T17:01:17.194Z',
'2020-10-21T23:36:17.929Z',
'2021-10-22T10:51:36.790Z',
],
};
const account2 = {
owner: 'Jessica Davis',
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
movementsDates: [
'2019-11-01T13:15:33.035Z',
'2019-11-30T09:48:16.867Z',
'2019-12-25T06:04:23.907Z',
'2021-01-25T14:18:46.235Z',
'2021-02-05T16:33:06.386Z',
'2021-10-20T14:43:26.374Z',
'2021-10-21T18:49:59.371Z',
'2021-10-22T12:01:20.894Z',
],
};
const account3 = {
owner: 'Steven Thomas Williams',
movements: [200, -200, 340, -300, -20, 50, 400, -460],
interestRate: 0.7,
pin: 3333,
movementsDates: [
'2019-11-01T13:15:33.035Z',
'2019-11-30T09:48:16.867Z',
'2019-12-25T06:04:23.907Z',
'2020-01-25T14:18:46.235Z',
'2020-02-05T16:33:06.386Z',
'2021-10-20T14:43:26.374Z',
'2021-10-21T18:49:59.371Z',
'2021-10-22T12:01:20.894Z',
],
};
const account4 = {
owner: 'Sarah Smith',
movements: [430, 1000, 700, 50, 90],
interestRate: 1,
pin: 4444,
movementsDates: [
'2019-11-18T21:31:17.178Z',
'2019-12-23T07:42:02.383Z',
'2020-01-28T09:15:04.904Z',
'2020-04-01T10:17:24.185Z',
'2020-05-08T14:11:59.604Z',
'2021-10-20T17:01:17.194Z',
'2021-10-21T23:36:17.929Z',
'2021-10-22T10:51:36.790Z',
],
};
const accounts = [account1, account2, account3, account4];
// 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');
// functions
const formatcur = function (value, locale) {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: 'USD',
}).format(value);
};
const formatdate = function (date) {
const calcdayspassed = (day1, day2) =>
Math.round(Math.abs(day2 - day1) / (24 * 60 * 60 * 1000));
const dayspassed = calcdayspassed(new Date(date), new Date());
console.log(dayspassed);
if (dayspassed === 0) return 'Today';
if (dayspassed === 1) return 'Yesterday';
if (dayspassed <= 7) return `${dayspassed} days ago`;
else {
// const dis_date = new Date(date);
// const day = `${dis_date.getDate()}`.padStart(2, 0);
// const month = `${dis_date.getMonth() + 1}`.padStart(2, 0);
// const year = dis_date.getFullYear();
// const display_date = `${day}/${month}/${year}`;
// return display_date;
console.log(date);
const locale = navigator.language;
return new Intl.DateTimeFormat(locale).format(new Date(date));
}
// return display_date;
// console.log(dayspassed);
};
// experminenting internationalisation
// const now = new Date();
// const option = {
// hour: 'numeric',
// minute: 'numeric',
// day: 'numeric',
// month: 'long',
// year: 'numeric',
// weekday: 'long',
// };
// const locale = navigator.language; // this we use to get the language of browser which we r using
// console.log(locale);
// labelDate.textContent = new Intl.DateTimeFormat(locale, option).format(now);
const displayMovements = function (accounts, sort = false) {
containerMovements.innerHTML = ''; // used to empty the html same as we done in textcontent
const movs = sort
? accounts.movements.slice().sort((a, b) => a - b)
: accounts.movements;
movs.forEach(function (mov, i) {
const type = mov > 0 ? 'deposit' : 'withdrawal';
const display_date = formatdate(accounts.movementsDates[i]);
const formattedmov = formatcur(mov, navigator.language);
// const formattedmovement = new Intl.NumberFormat(navigator.language, {
// style: 'currency',
// currency: 'USD',
// }).format(mov);
// console.log(formattedmovement);
const html = `<div class="movements__row">
<div class="movements__type movements__type--${type}">${i + 1} ${type}</div>
<div class="movements__date">${display_date}</div>
<div class="movements__value">${formattedmov}</div>
</div>`;
// .insertAdjacentHTML used to add html
containerMovements.insertAdjacentHTML('afterbegin', html);
});
};
//displayMovements(account1.movements);
const createusername = function (acc) {
acc.forEach(function (accs) {
accs.username = accs.owner
.toLowerCase()
.split(' ')
.map(function (name) {
return name[0];
})
.join('');
});
};
createusername(accounts);
const updateui = function (acc) {
// DISPLAY MOVEMENT
displayMovements(acc);
// DISPLAY BALANCE
calcdisplaysum(acc);
// DISPLAY SUMMARY
calcdisplaybal(acc);
};
//calcprintbal(account1.movements);
//console.log(containerMovements.innerHTML);
const calcdisplaysum = function (acc) {
const credit = acc.movements
.filter(mov => mov > 0)
.reduce((sum, cre) => sum + cre, 0);
const formattedcredit = formatcur(credit, navigator.language);
labelSumIn.textContent = `${formattedcredit}`;
const debit = acc.movements
.filter(deb => deb < 0)
.reduce((deb, mov) => deb + mov, 0);
const formatteddebit = formatcur(debit, navigator.language);
labelSumOut.textContent = `${formatteddebit}`;
const interest = acc.movements
.filter(mov => mov > 0)
.map(deposit => (acc.interestRate * 1.2) / 100)
.reduce((add, int) => add + int, 0);
const formattedint = formatcur(interest, navigator.language);
labelSumInterest.textContent = `${formattedint}`;
};
//calcdisplaybalsum(account1.movements);
const calcdisplaybal = function (acc) {
acc.balance = acc.movements.reduce((accu, mov) => accu + mov, 0);
const formattedbal = formatcur(acc.balance, navigator.language);
labelBalance.textContent = `${formattedbal}`;
};
const startlogouttimer = function () {
// SETTING TIMER TO 100 SEC
const ticking = function () {
const min = String(Math.trunc(time / 60)).padStart(2, 0);
const sec = String(time % 60).padStart(2, 0);
// IN EACH CALL PRINT REM TIME
labelTimer.textContent = `${min}:${sec}`;
// IF TIMER REACHES 0 SECONDS LOGOUT USER and stop timer
if (time === 0) {
clearTimeout(timer);
// labelWelcome.textContent = 'Log in to get started';
containerApp.style.opacity = 0;
}
// decrease 1 second
time--;
};
let time = 300;
// CALL THE TIMER EVERY SECOND
ticking();
const timer = setInterval(ticking, 1000);
return timer;
};
// EVENT HANDLER
let current_account, timer;
btnLogin.addEventListener('click', function (e) {
// prevent form from submitting i.e doesn't reload page when hit submit button
e.preventDefault();
current_account = accounts.find(
acc => acc.username === inputLoginUsername.value
);
// console.log(current_account);
if (current_account?.pin === Number(inputLoginPin.value)) {
// DISPLAY UI AND MESSAGE
labelWelcome.textContent = `Welcome Back, ${
current_account.owner.split(' ')[0]
}`;
containerApp.style.opacity = 100;
const now = new Date();
const option = {
hour: 'numeric',
minute: 'numeric',
day: 'numeric',
month: 'long',
year: 'numeric',
weekday: 'long',
};
const locale = navigator.language;
// console.log(locale);
labelDate.textContent = new Intl.DateTimeFormat(locale, option).format(now);
// const curr_date = new Date();
// const day = `${curr_date.getDate()}`.padStart(2, 0);
// const month = `${curr_date.getMonth()}`.padStart(2, 0);
// const year = curr_date.getFullYear();
// const hour = `${curr_date.getHours()}`.padStart(2, 0);
// const min = `${curr_date.getMinutes()}`.padStart(2, 0);
// // displaying in day month nd year format
// labelDate.textContent = `${day}/${month}/${year}, ${hour}:${min}`;
// CLEARING INPUT FIELDS
inputLoginUsername.value = inputLoginPin.value = '';
inputLoginPin.blur();
if (timer) clearInterval(timer);
timer = startlogouttimer();
// UPDATE UI
updateui(current_account);
}
});
btnTransfer.addEventListener('click', function (e) {
e.preventDefault();
const amount = Number(inputTransferAmount.value);
const reciever_account = accounts.find(
acc => acc.username === inputTransferTo.value
);
inputTransferAmount.value = inputTransferTo.value = '';
if (
amount > 0 &&
reciever_account &&
current_account.balance >= amount &&
reciever_account?.username !== current_account.username
) {
// DOING TRANSFER
current_account.movements.push(-amount);
reciever_account.movements.push(amount);
// adding date to reciver account
current_account.movementsDates.push(new Date());
reciever_account.movementsDates.push(new Date());
updateui(current_account);
//reset timer
clearInterval(timer);
timer = startlogouttimer();
}
});
btnLoan.addEventListener('click', function (e) {
e.preventDefault();
const loan_amount = Number(inputLoanAmount.value);
if (
loan_amount > 0 &&
current_account.movements.some(mov => mov >= 0.1 * loan_amount)
) {
setTimeout(() => {
current_account.movements.push(loan_amount);
current_account.movementsDates.push(new Date());
updateui(current_account);
// reset timer
clearInterval(timer);
timer = startlogouttimer();
}, 300);
// current_account.movements.push(loan_amount);
// current_account.movementsDates.push(new Date());
// updateui(current_account);
}
inputLoanAmount.value = '';
});
btnClose.addEventListener('click', function (e) {
e.preventDefault();
const account_close_user = inputCloseUsername.value;
const account_close_pin = Number(inputClosePin.value);
if (
account_close_user &&
account_close_pin &&
current_account.username === account_close_user &&
current_account.pin === account_close_pin
) {
const index = accounts.findIndex(
acc => acc.username === current_account.username
);
// DELETING ACCOUNT
accounts.splice(index, 1);
// HIDE UI
containerApp.style.opacity = 0;
}
inputCloseUsername.value = inputClosePin.value = '';
});
let sorted_state = false;
btnSort.addEventListener('click', function (e) {
e.preventDefault();
// console.log('working');
displayMovements(current_account, !sorted_state);
sorted_state = !sorted_state;
});
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// LECTURES
// const currencies = new Map([
// ['USD', 'United States dollar'],
// ['EUR', 'Euro'],
// ['GBP', 'Pound sterling'],
// ]);
// const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
// /////////////////////////////////////////////////
// let arr = ['a', 'b', 'c', 'd', 'e'];
// // SLICE METHOD
// console.log(arr.slice(2));
// console.log(arr.slice(2, 3));
// console.log(arr.slice(0, -3));
// console.log([...arr]);
// // SPLICE METHOD
// //console.log(arr.splice(2));
// arr.splice(-1);
// console.log(arr);
// arr.splice(1, 2);
// console.log(arr);
// // REVERSE
// arr = ['a', 'b', 'c', 'd', 'e'];
// const arr2 = ['f', 'g', 'h', 'i', 'j'];
// console.log(arr2.reverse());
// console.log(arr2);
// // CONCAT
// const letters = arr.concat(arr);
// console.log(letters);
// console.log([...arr, ...arr2]);
// //JOIN
// console.log(letters.join('-'));
// const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
// for (const i in movements) {
// console.log(movements[i]);
// }
// for (const [i, movement] of movements.entries()) {
// if (movement > 0) {
// console.log(`Movement ${i + 1}: you deposited ${movement}`);
// } else {
// console.log(`Movement ${i + 1}:you withdraw ${Math.abs(movement)}`);
// }
// }
// console.log('--------------FOR EACH----------------');
// movements.forEach(function (movement, index, array) {
// if (movement > 0) {
// console.log(`Movement ${index + 1}:you deposited ${movement}`);
// } else {
// console.log(`Movement ${index + 1}:you withdraw ${Math.abs(movement)}`);
// }
// });
//MAP
// const currencies = new Map([
// ['USD', 'United States dollar'],
// ['EUR', 'Euro'],
// ['GBP', 'Pound sterling'],
// ]);
// currencies.forEach(function (value, key, m) {
// console.log(`${key}: ${value}`);
// });
// // SET
// const currenciesunique = new Set(['USD', 'GBP', 'USD', 'EUR', 'EUR']);
// console.log(currenciesunique);
// currenciesunique.forEach(function (value, _, set) {
// console.log(`${value}: ${value}`); // set doesn't have key thats's why key==values
// });
// const checkdogs = function (dogjulia, dogkate) {
// const djuliacorr = dogjulia.slice();
// djuliacorr.splice(0, 1);
// djuliacorr.splice(-2);
// // or we can also do this djuliacorr.slice(1,3);
// const dogs = djuliacorr.concat(dogkate);
// dogs.forEach(function (dog, i) {
// if (dog >= 3) {
// console.log(`dog number ${i + 1} is adult with ${dog} years of age`);
// } else {
// console.log(`dog number ${i + 1} is puppy with ${dog} years of age 🐶`);
// }
// });
// };
// checkdogs([3, 5, 2, 12, 7], [4, 1, 15, 8, 3]);
// map method
// const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
// const eurtousd = 1.1;
// // const movementsusd = movements.map(function (mov) {
// // return mov * eurtousd;
// // });
// console.log(movements);
// //console.log(movementsusd);
// const moveusd = [];
// for (const mov of movements) {
// moveusd.push(mov * eurtousd);
// }
// console.log(moveusd);
// const movementsusd = movements.map(mov => mov * eurtousd);
// console.log(movementsusd);
// const movdesc = movements.map(function (mov, i) {
// return `Movement ${i + 1}:you ${mov > 0 ? 'deposited' : 'withdraw'} ${mov}`;
// });
// console.log(movdesc);
// const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
// const deposits = movements.filter(function (mov) {
// return mov > 0;
// });
// console.log(movements);
// console.log(deposits);
// const withdraw = movements.filter(function (mov) {
// return mov < 0;
// });
// console.log(withdraw);
// const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
// console.log(
// movements.reduce(function (accu, cur, i, mov) {
// return accu + cur;
// }, 0) // 0 defines the initial value for accu
// );
// // maximum value
// const max = movements.reduce(function (maxi, mov) {
// return mov > maxi ? mov : maxi;
// }, movements[0]);
// console.log(max);
// const calcage = function (t1) {
// const humanage = t1.map(function (age) {
// if (age <= 2) return 2 * age;
// else return 16 + 4 * age;
// });
// console.log(humanage);
// const adult = humanage.filter(function (age) {
// return age > 18;
// });
// const ans = adult.reduce(function (sum, age) {
// return sum + age / adult.length;
// }, 0);
// console.log(ans);
// };
// calcage([5, 2, 4, 1, 15, 8, 3]);
// const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
// console.log(movements.find(mov => mov < 0));
// console.log(accounts);
// const acc = accounts.find(acco => acco.owner === 'Jessica Davis');
// console.log(acc);
// const a = [5, 4, 3, 2, 1];
// a.sort((a, b) => a - b);
// console.log(...a);
// const dogs = [
// { weight: 22, curfood: 250, owners: ['Alice', 'Bob'] },
// { weight: 8, curfood: 200, owners: ['Matilda'] },
// { weight: 13, curfood: 275, owners: ['Sarah', 'Jon'] },
// { weight: 32, curfood: 340, owners: ['Michael'] },
// ];
// dogs.forEach(dog => (dog.recFood = Math.trunc(dog.weight ** 0.75 * 28)));
// console.log(dogs);
// const dogsarah = dogs.find(dog => dog.owners.includes('Sarah'));
// console.log(dogsarah);
// console.log(
// `Sarah dog is eating ${
// dogsarah.recFood < dogsarah.curfood ? 'too much' : 'less'
// }`
// );
// let ownerseat2much = [];
// let ownerseat2less = [];
// dogs.forEach(dog =>
// dog.curfood > dog.recFood
// ? ownerseat2much.push(dog.owners)
// : ownerseat2less.push(dog.owners)
// );
// ownerseat2less = ownerseat2less.flat();
// ownerseat2much = ownerseat2much.flat();
// console.log(`${ownerseat2much.join(' and ')} eats 2 much`);
// console.log(`${ownerseat2less.join(' and ')} eats 2 less`);
// const da = {
// move: '2020-11-18T21:31:17.178z',
// };
// console.log(new Date(da.move).getMonth());
// internationalisation on numbers for formatting
// const option = {
// style: 'currency',
// unit: 'celsius',
// currency: 'EUR',
// };
// const num = 7234723.87;
// console.log(new Intl.NumberFormat('en-US', option).format(num));
// console.log(new Intl.NumberFormat('de-DE', option).format(num));
// console.log(new Intl.NumberFormat(navigator.language, option).format(num));
// TIMER IN JS
// SET TIMEOUT FUNC
// const ing = ['olive', 'peas'];
// const pizzt = setTimeout(
// (ing1, ing2) => console.log(`here is your pizza with ${ing1} and ${ing2}`),
// 3000,
// ...ing
// );
// console.log('waiting');
// if (ing.includes('pea')) clearTimeout(pizzt);
// SET INTERVAL FUNC
// setInterval(() => {
// const now = new Date();
// console.log(now.getHours(), now.getMinutes(), now.getSeconds());
// }, 1000);