Last active
March 15, 2023 18:14
-
-
Save mjmeilahn/31a962c82ced68ddcc5adbdf24d93362 to your computer and use it in GitHub Desktop.
JS: Comparing Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compare TWO strings and return TRUE if the 2nd String | |
// only requires one change to match the original string. | |
// Otherwise return FALSE. | |
const compareStrings = (orig, diff) => { | |
let count = 0 | |
diff.split('').map(char => { | |
const match = orig.split('').find(i => i === char) | |
if (match !== undefined) { | |
count += 1 | |
} | |
}) | |
return (count === (orig.length - 1)) ? true : false | |
} | |
console.log(compareStrings('make', 'rake')) // true | |
console.log(compareStrings('make', 'mke')) // true | |
console.log(compareStrings('make', 'ake')) // true | |
console.log(compareStrings('make', 'ekam')) // false | |
console.log(compareStrings('make', 'make')) // false | |
console.log(compareStrings('make', 'rare')) // false | |
console.log(compareStrings('make', 'ke')) // false | |
console.log(compareStrings('make', 'ma')) // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment