- Trending Categories
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
Finding matches in two elements JavaScript
We are required to write a function that returns true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example,
["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring their case.
The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".
Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
This is a fairly simple problem; we will just split the second element of the array and iterate over the array thus produced to check whether the first element contains all the characters or not.
Example
const arrayContains = ([fist, second]) => { return second .toLowerCase() .split("") .every(char => { return fist.toLowerCase().includes(char); }); }; console.log(arrayContains(['hello', 'HELLO'])); console.log(arrayContains(['hello', 'hey'])); console.log(arrayContains(['Alien', 'line']));
Output
The output in the console will be −
true false true
Advertisements