I want to check if the user and pass is matched and also their indexes since im using array of objects im using localstorage and javascriptfor this activity
//find user if exist
var founduser = accounts.find(e =>{
return e.user === user
})
//find pass if exist
var foundpass = accounts.find(e =>{
return e.pass === pass
})
//find index of user
var indexofuser = accounts.indexOf(founduser)
//find index of pass
var indexofpass = accounts.indexOf(foundpass)
//check if either 2 of them is undefined
if( foundpass != undefined || founduser != undefined){
//check if the index are matched
if( indexofuser === indexofpass){
alert("logged in successfully")
}
//return an alert if its not matched
else{
alert("Wrong Password Please try again")
}
}
//return an alert if on of it is undefined
else{
alert("Wrong Password Please try again")
}
2
Answers
You can use
.find()
but make your callback function only return true if both the account’suser
andpass
properties match:This should give you the same behaviour as your existing code as you’re logging the same thing in both
else
blocks. However, do consider the following security implications of:.find()
can potentially open you up to timing attacks which allows the user to guess what the username / passwords are based on how long your code takes to runSo don’t use the above code in production for implementing a username + password system. Use it as an exercise to learn how to find objects in an array that match criteria.
}