Skip to content

Instantly share code, notes, and snippets.

@runandrerun
Created December 16, 2018 20:18
Show Gist options
  • Save runandrerun/b6af64d7e4425624cd9884e7c0e27cdb to your computer and use it in GitHub Desktop.
Save runandrerun/b6af64d7e4425624cd9884e7c0e27cdb to your computer and use it in GitHub Desktop.
const characterMatch = (stringA, stringB) => {
// basic optimization - store the lengths
const aLength = stringA.length;
const bLength = stringB.length;
// brute force: double for loops
// loop over the first argument (stringA)
for (let i = 0; i < aLength; i++) {
// for every index in stringA, loop over stringB
for (let k = 0; k < bLength; k++) {
// during every loop, every index of stringB is compared
// to the index of stringA. return true if it's a match
if (stringA[i] === stringB[k]) {
return true;
}
}
}
// always return false if there's no match
return false;
};
const HELLO = "Hello";
const GOODBYE = "Goodbye";
characterMatch(HELLO, GOODBYE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment