Files
30-seconds-of-code/snippets_archive/isSimilar.md
Angelos Chalaris 611729214a Snippet format update
To match the starter (for the migration)
2019-08-13 10:29:12 +03:00

749 B

title, tags
title tags
isSimilar string,intermediate

Determines if the pattern matches with str.

Use String.toLowerCase() to convert both strings to lowercase, then loop through str and determine if it contains all characters of pattern and in the correct order. Adapted from here.

const isSimilar = (pattern, str) =>
  [...str].reduce(
      (matchIndex, char) =>
          char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase()
              ? matchIndex + 1
              : matchIndex,
      0
  ) === pattern.length;
isSimilar('rt','Rohit'); // true
isSimilar('tr','Rohit'); // false