1

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;
}
asked Jul 11, 2021 at 19:03
0

3 Answers 3

3

you need to write w in lower case enter image description here

answered Jul 11, 2021 at 19:08
Sign up to request clarification or add additional context in comments.

1 Comment

wow! that will do it! thanks so much I spent hours trying to figure it out. I just need it a new pair of fresh eyes!
1

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.

answered Jul 11, 2021 at 19:07

1 Comment

Thank you this was super helpful.
0

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.

answered Jul 11, 2021 at 19:07

1 Comment

Thanks for looking at it and for the advise. I am in the process of learning typescript.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.