Reputation: 77
I am trying to loop through the array of objects below. I created a function to loop through it and return true if the username and password exist in the dataBase, else return false. However, it always returns false whether the username and password exist or not.
what could possible be wrong with it? Any hints would be super helpful!
const dataBase =[
{
username:'cristiano',
password:'hattrick'
},
{
username:'cierra',
password:'lapuerta'
},
{
username:'carlos',
password:'elpollo'
}
];
function checkDataBase(userName, passWord){
for (let i=0; i < dataBase.length; i++){
if (dataBase[i].username === userName && dataBase[i].passWord === passWord){
return true;
}
}
return false;
}
Upvotes: 1
Views: 369
Reputation: 2175
just check the case of your variables. This test would work.
if (dataBase[i].username === userName && dataBase[i].password === passWord){
i would advise you to use eslint and typescript in your project or IDE. He would have pointed the error for you before execution.
Upvotes: 0
Reputation: 506
This check is not good because your user objects have a 'password' property and yet within the if statement you are trying to access the 'passWord' property which is non existent.
if (dataBase[i].username === userName && dataBase[i].passWord === passWord)
Capitalization matters in javascript.
Upvotes: 1