Finding matches in two elements JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

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.


const arrayContains = ([fist, second]) => {
   return second
   .every(char => {
      return fist.toLowerCase().includes(char);
console.log(arrayContains(['hello', 'HELLO']));
console.log(arrayContains(['hello', 'hey']));
console.log(arrayContains(['Alien', 'line']));


The output in the console will be −

Published on 26-Aug-2020 14:48:54