Skip to content

Instantly share code, notes, and snippets.

@mjmeilahn
Last active March 15, 2023 18:14
Show Gist options
  • Save mjmeilahn/31a962c82ced68ddcc5adbdf24d93362 to your computer and use it in GitHub Desktop.
Save mjmeilahn/31a962c82ced68ddcc5adbdf24d93362 to your computer and use it in GitHub Desktop.
JS: Comparing Strings
// 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