Validating email and password - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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
raja
Published on 01-Oct-2020 13:46:38
Advertisements