- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements