Validating email and password - JavaScript


Suppose, we have this dummy array that contains the login info of two of many users of a social networking platform −

const array = [{
   email: 'usman@gmail.com',
   password: '123'
 },
 {
   email: 'ali@gmail.com',
   password: '123'
 }
];

We are required to write a JavaScript function that takes in an email string and a password string.

The function should return a boolean based on the fact whether or not the user exists in the database.

Example

Following is the code −

const array = [{
   email: 'usman@gmail.com',
   password: '123'
}, {
   email: 'ali@gmail.com',
   password: '123'
}];
const matchCredentials = (email, password) => {
   const match = array.find(el => {
      return el.email === email && el.password === password;
   });
   return !!match;
};
console.log(matchCredentials('usman@gmail.com', '123'));
console.log(matchCredentials('usman@gmail.com', '1423'));

This will produce the following output on console −

true
false

Updated on: 01-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements