-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
291 lines (267 loc) · 8.8 KB
/
Copy pathscript.js
File metadata and controls
291 lines (267 loc) · 8.8 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
'use strict';
// BANKIST APP
// Data
const account1 = {
owner: 'Jonas Schmedtmann',
movements: [200, 450, -400, 3000, -650, -130, 70, 1300],
interestRate: 1.2, // %
pin: 1111,
};
const account2 = {
owner: 'Jessica Davis',
movements: [5000, 3400, -150, -790, -3210, -1000, 8500, -30],
interestRate: 1.5,
pin: 2222,
};
const account3 = {
owner: 'Steven Thomas Williams',
movements: [200, -200, 340, -300, -20, 50, 400, -460],
interestRate: 0.7,
pin: 3333,
};
const account4 = {
owner: 'Sarah Smith',
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 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');
/////////////////////////////////////////////////
/////////////////////////////////////////////////
// LECTURES
const currencies = new Map([
['USD', 'United States dollar'],
['EUR', 'Euro'],
['GBP', 'Pound sterling'],
]);
const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
/////////////////////////////////////////////////
//creating usernames for login
function createUsername(accs) {
accs.forEach(acc => {
acc.username = acc.owner
.toLowerCase()
.split(' ')
.map(value => value[0])
.join('');
});
}
createUsername(accounts);
//
//
let displayMovement = (movements, sort = false) => {
containerMovements.innerHTML = '';
let mov = sort ? movements.slice().sort((a, b) => a - b) : movements;
mov.forEach((value, index) => {
let tranType = value > 0 ? 'deposit' : 'withdrawal';
let html = `
<div class="movements__row">
<div class="movements__type movements__type--${tranType}">
${index + 1} ${tranType}</div>
<div class="movements__date"> </div>
<div class="movements__value">${value}€</div>
</div>
`;
containerMovements.insertAdjacentHTML('afterbegin', html);
});
};
//
//
let calDisplayBal = acc => {
let balance = acc.movements.reduce((prevcurr, curr) => prevcurr + curr, 0);
acc.balance = balance;
labelBalance.textContent = `${acc.balance} €`;
};
//calDisplayBal(accounts);
//
let calDisplaySummary = function (account) {
//console.log(account);
//console.log(account.movements);
let incomes = account.movements
.filter(mov => mov > 0)
.reduce((acc, curr) => acc + curr);
labelSumIn.textContent = `${incomes} €`;
let outcome = account.movements
.filter(mov => mov < 0)
.reduce((acc, curr) => acc + curr, 0);
labelSumOut.textContent = `${Math.abs(outcome)} €`;
let interest = account.movements
.filter(mov => mov > 0)
.map(deposit => (deposit * account.interestRate) / 100)
.filter(int => int >= 1)
.reduce((acc, curr) => acc + curr, 0);
//console.log({ interest });
labelSumInterest.textContent = `${interest} €`;
};
//login functionality
let currentAccount;
btnLogin.addEventListener('click', e => {
//prevent form from submitting
e.preventDefault();
//console.log('LOGIN');
//checking for account-object in accounts array to see if it matches username input
currentAccount = accounts.find(
acc => acc.username === inputLoginUsername.value
);
console.log(currentAccount);
//checking for current account pin
if (currentAccount?.pin === Number(inputLoginPin.value)) {
console.log('LOGIN');
//display ui and message
labelWelcome.textContent = `Welcome back, ${currentAccount.owner}`;
containerApp.style.opacity = '100';
//display bal
calDisplayBal(currentAccount);
//display movements
displayMovement(currentAccount.movements);
//display summary
calDisplaySummary(currentAccount);
//clear fields
inputLoginUsername.value = '';
inputLoginPin.value = '';
}
});
//
//transfer functionality
btnTransfer.addEventListener('click', e => {
e.preventDefault();
let amount = Number(inputTransferAmount.value);
let recieverAcc = inputTransferTo.value;
let recieverobj = accounts.find(acc => acc.username === recieverAcc);
inputTransferAmount.value = inputLoginUsername.value = '';
if (
currentAccount.balance > 0 &&
recieverAcc &&
currentAccount.balance >= amount &&
recieverAcc.username !== currentAccount.username
) {
recieverobj.movements.push(amount);
currentAccount.movements.push(-amount);
displayMovement(currentAccount.movements);
calDisplayBal(currentAccount);
calDisplaySummary(currentAccount);
}
});
//
//the find method returns the first item that returns true
//close account using the findindex method
btnClose.addEventListener('click', e => {
e.preventDefault();
currentAccount = accounts.find(
acc => acc.username === inputCloseUsername.value
);
if (
currentAccount?.username === inputCloseUsername.value &&
currentAccount?.pin === Number(inputClosePin.value)
) {
let index = accounts.findIndex(
acc => acc.username === currentAccount.username
);
accounts.splice(index, 1);
containerApp.style.opacity = '0';
inputCloseUsername.value = inputClosePin.value = '';
currentAccount = {};
}
});
//loan functionality
btnLoan.addEventListener('click', e => {
e.preventDefault();
let lA = Number(inputLoanAmount.value);
if (currentAccount.movements.some(mov => mov >= (lA * 10) / 100)) {
currentAccount.movements.push(lA);
displayMovement(currentAccount.movements);
calDisplayBal(currentAccount);
calDisplaySummary(currentAccount);
inputLoanAmount.value = '';
}
});
//
const movementDescription = movements.map((mov, i, arr) => {
return `Movement ${i + 1}: You ${
mov > 0 ? 'Deposited' : 'Withdrew'
} ${Math.abs(mov)}`;
});
let eurToUsd = 1.1;
let totalDepositUSD = movements
.filter(mov => mov > 0)
.map(mov => mov * eurToUsd)
.reduce((acc, mov) => acc + mov, 0);
let firstWithdrawal = movements.find(mov => mov < 0);
let account = accounts.find(acc => acc.owner === 'Jessica Davis');
//flat and flatmap
function totalDepoBal(acc) {
accMov = acc
.map(mov => mov.movements)
.flat()
.filter(mov => mov > 0)
.reduce((acc, curr) => acc + curr, 0);
}
//sort functionality
let sorted = false;
btnSort.addEventListener('click', e => {
e.preventDefault();
displayMovement(currentAccount.movements, !sorted);
});
//////////////////////////////
//coding challenge
// let dogsJulia = [3, 6, 1, 8];
// let dogsKate = [9, 7, 1, 4];
// function checkDogs(dogsJulia, dogsKate) {
// let juliaCopy = dogsJulia.slice(1, -2);
// console.log(juliaCopy);
// let bothData = [...juliaCopy, ...dogsKate];
// console.log(bothData);
// bothData.forEach((value, index) => {
// //console.log(value);
// value >= 3
// ? console.log(
// `Dog number ${index + 1} is an 'ADULT', and is ${value} years old`
// )
// : console.log(
// `Dog number ${index + 1} is an 'PUPPY', and is ${value} years old`
// );
// });
// }
// checkDogs(dogsJulia, dogsKate);
// function dogAgeYears(params) {
// let humanAge = params.map(value => (value <= 2 ? 2 * value : 16 + value * 4));
// console.log(humanAge);
// let check = humanAge.filter(values => values >= 18);
// let avg = check.reduce((acc, curr) => {
// return (acc += curr);
// });
// let mainAvg = avg / check.length;
// console.log(mainAvg);
// }
// dogAgeYears([5, 2, 4, 1, 15, 8, 3]);
// dogAgeYears([16, 6, 10, 5, 6, 1, 4]);
// let calAvgHumange = params =>
// params
// .map(value => (value <= 2 ? 2 * value : 16 + value * 4))
// .filter(value => value >= 18)
// .reduce((acc, curr, i, arr) => acc + curr / arr.length, 0);
// console.log(calAvgHumange([5, 2, 4, 1, 15, 8, 3]));
// calAvgHumange([16, 6, 10, 5, 6, 1, 4]);
//