const calcDisplayBalance = function (movements) {
const balance = movements.reduce((acc, mov) => acc + mov, 0);
labelBalance.textContent = `${balance}€`;
};
const clacDisplaySummary = function (acc) {
const income = acc.movements
.filter(mov => mov > 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumIn.textContent = `${income}€`;
const out = acc.movements
.filter(mov => mov < 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumOut.textContent = `${Math.abs(out)}€`;
const interest = acc.movements
.filter(mov => mov > 0)
.map(deposite => (deposite * acc.interestRate) / 100)
.filter((int, i, arr) => {
return int >= 1;
})
.reduce((acc, int) => acc + int, 0);
labelSumInterest.textContent = `${interest}€`;
};
const createUsernames = function (accs) {
accs.forEach(function (acc) {
acc.username = acc.owner
.toLowerCase()
.split(' ')
.map(name => name[0])
.join('');
});
};
createUsernames(accounts);
let currentAccount;
btnLogin.addEventListener('click', function (e) {
e.preventDefault();
currentAccount = accounts.find(
acc => acc.username === inputLoginUsername.value
);
console.log(currentAccount);
if (currentAccount?.pin === Number(inputLoginPin.value)) {
labelWelcome.textContent = `Welcome back, ${
currentAccount.owner.split(' ')[0]
}`;
containerApp.style.opacity = 100;
inputLoginUsername.value = inputLoginPin.value = '';
inputLoginPin.blur();
displayMovements(currentAccount.movements);
calcDisplayBalance(currentAccount.movements);
clacDisplaySummary(currentAccount);
}
});

