-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
259 lines (217 loc) · 8.47 KB
/
script.js
File metadata and controls
259 lines (217 loc) · 8.47 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
'use strict';
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// BANKIST APP
// Data
const account1 = {
owner: 'Sasuke Uchiha',
movements: [200, 450, -400, 3000, -650, -130, 70, 1300, 100000],
interestRate: 1.2, // %
pin: 1111,
};
const account2 = {
owner: 'Naruto Uzumaki',
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
};
const account3 = {
owner: 'Kakashi Hatake',
movements: [200, -200, 340, -300, -20, 50, 400, -460],
interestRate: 0.7,
pin: 3333,
};
const account4 = {
owner: 'Hinata Hyuga',
movements: [430, 1000, 700, 50, 90],
interestRate: 1,
pin: 4444,
};
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 labelTransferWrongMsg = document.querySelector('.wrongMsg')
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');
// -------------------------------------------
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// ============= DISPLAY MOVEMENTS =================
const displayMovements = function (movements) {
containerMovements.innerHTML = '';
movements.forEach((movs, i) => {
const type = movs > 0 ? 'deposit' : 'withdrawal';
const html = `
<div class="movements__row">
<div class="movements__type movements__type--${type}">${
i + 1
}. ${type}</div>
<div class="movements__date">3 days ago</div>
<div class="movements__value">${Math.abs(movs)} €</div>
</div>`;
containerMovements.insertAdjacentHTML('afterbegin', html);
});
};
// displayMovements(account1.movements);
// -------------------------------------------
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// ============= CREATING USERNAME =================
// First we create this basic function to make a username
// const createUserName = function (acc) {
// const userName = acc
// .toLowerCase()
// .split(' ')
// .map(name => name[0])
// .join(''); // do the chaining one by one to grasp the situation
// return userName;
// };
// console.log(createUserName('Estiak Dewan Emon'));
// now we can dinamically use this function to passs into the accounts array & creating a username property inside each account object
const createUserName = function (accs) {
accs.forEach(function (acc) {
acc.username = acc.owner //creating username variable & implementing on owner property
.toLowerCase()
.split(' ')
.map(name => name[0]) // this map method creating a new array without mutating the original
.join('');
});
};
createUserName(accounts);
// console.log(accounts);
// -------------------------------------------
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// ============= DISPLAY Summary =================
const calcDisplaySummary = function (acc) {
const incomes = acc.movements
.filter(mov => mov > 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumIn.textContent = `${incomes} €`;
const withdraw = acc.movements
.filter(mov => mov < 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumOut.textContent = `${Math.abs(withdraw)} €`;
// Lets fix an 1.2% interest for our program on every deposit
// suppose bank add an condition recently on interest rate
// only if an interest that is bigger than one than it will be added to the total value
// so where should we add this in the chai? (yes the ans is in between where we calculating and adding the interest rate)
const interest = acc.movements
.filter(mov => mov > 0)
.map(deposit => (deposit * acc.interestRate) / 100)
.filter((interest, i, arr) => {
// console.log(arr); // check out the array for debugging / understanding this part
return interest >= 1;
})
.reduce((acc, interest) => acc + interest, 0);
labelSumInterest.textContent = `${interest} €`;
};
// calcDisplaySummary(account1.movements);
// -------------------------------------------
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// ============= DISPLAY TOTAL BALANCE =================
const displayTotalBalance = function (acc) {
const balance = acc.movements.reduce(function (acc, mov) {
return acc + mov;
}, 0);
// crating balance property for accounts object to hold value
acc.balance = balance;
labelBalance.textContent = `${balance} €`;
};
// displayTotalBalance(account1.movements);
// -------------------------------------------
/////////////////////////////////////////////////
// ============= Update Ui funtion =================
const updateUi = function (acc) {
// Display Movements
displayMovements(acc.movements);
// Display Balance
displayTotalBalance(acc);
// Display Summary
calcDisplaySummary(acc);
};
// -------------------------------------------
/////////////////////////////////////////////////
// ============= User log in =================
let currentAccout;
btnLogin.addEventListener('click', function (e) {
// to prevent the form from submitting
e.preventDefault();
currentAccout = accounts.find(
acc => acc.username === inputLoginUsername.value
);
// console.log(currentAccout);
// if the username doesn't match value it will return an error that why we can use optional chaining in the condition
// if current account is true, mean if the current username exit than it will check for pin
if (currentAccout?.pin === Number(inputLoginPin.value)) {
// Display Ui and message
labelWelcome.textContent = `Welcome back, ${
currentAccout.owner.split(' ')[0]
}`;
// at first the app class opacity will be 0
// but after a successful log in it will show the user interface
containerApp.style.opacity = 100;
// containerApp.style.pointerEvents = auto;
// containerApp.style.visibility = visible;
// clearing input fields
inputLoginUsername.value = '';
inputLoginPin.value = '';
inputLoginPin.blur();
// // Display Movements
// displayMovements(currentAccout.movements);
// // Display Balance
// displayTotalBalance(currentAccout);
// // Display Summary
// calcDisplaySummary(currentAccout);
// Resusable function to update Ui
updateUi(currentAccout);
}
});
// -------------------------------------------
/////////////////////////////////////////////////
// ============= TRANSFER MONEY =================
btnTransfer.addEventListener('click', function (e) {
e.preventDefault();
const amount = Number(inputTransferAmount.value);
const receiverAccount = accounts.find(
acc => acc.username === inputTransferTo.value
);
inputTransferAmount.value = '';
inputTransferTo.value = '';
inputTransferAmount.blur();
if (
amount > 0 &&
receiverAccount &&
currentAccout.balance >= amount &&
receiverAccount &&
receiverAccount.username !== currentAccout.username
) {
currentAccout.movements.push(-amount);
receiverAccount.movements.push(amount);
updateUi(currentAccout);
labelTransferWrongMsg.textContent = '';
} else {
labelTransferWrongMsg.textContent = 'Wrong! Try again'
labelTransferWrongMsg.style.color = "#A34343";
}
});
// -------------------------------------------