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.

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
raja
Published on 26-Aug-2020 11:18:54
Advertisements