Files
30-seconds-of-code/snippets_archive/isSimilar.md
Rohit Tanwar a47f64c89a update
2018-02-26 15:06:47 +05:30

659 B

isSimilar

Determines if the pattern matches with str

Loops through str and determines if it contains all characters of pattern and in the correct order. Both the strings are converted to lower case.

Adapted from here.

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