I'm working on a school project and am in need of some guidance. I have issues identifying the fault making both my if and else alerts show up when I click my login button. It only recognises the first user in the array as well.
When I've tried this without an array it works, but as I want to have two users I find it's probably more apt to use and array, right? I've tried making the loginButton click event an arrow function, as well as making my else an else if but I'm not having much luck with that either. I essentially just want the exact same result but without triggering the else alert when my if statement is true, as well as being able to log in with either of my user credentials. I'm very much a novice at this fine art so please be gentle. Thankful for any help!
let loginButton = document.getElementById('login-button');
let userCred = [
{
username: 'Peregrin',
password: 'SecondBreakfast'
},
{
username: 'Meriadoc',
password: 'Elevenses'
}];
loginButton.addEventListener('click', loginUser);
function loginUser(){
let username = document.getElementById('user').value;
let password = document.getElementById('password').value;
for(i = 0; i < userCred.length; i++){
if(username == userCred[i].username && password == userCred[i].password){
window.location.assign('bank.html');
alert('Login Successful');
} else {
alert('Invalid information');
}
}
};