Broken Keyboard (Code Challenge)

  • Page Owner: Not Set
  • Last Reviewed: 2019-12-06

Broken Keyboard

Given what is supposed to be typed and what is actually typed, write a function that returns the broken key(s). The function looks like:

findBrokenKeys(correct phrase, what you actually typed)

Examples

findBrokenKeys("happy birthday", "hawwy birthday") ➞ ["p"]

findBrokenKeys("starry night", "starrq light") ➞ ["y", "n"]

findBrokenKeys("beethoven", "affthoif5") ➞ ["b", "e", "v", "n"]

Notes

  • Broken keys should be ordered by when they first appear in the sentence.
  • Only one broken key per letter should be listed.
  • Letters will all be in lower case.

Tests

Test = (x, y) => x == y ? console.log("Pass", "Yours", x, "Correct:", y) || true : console.log("Fail", "Yours", x, "Correct:", y) || false;

findBrokenKeys = x => !1;

Test(findBrokenKeys("happy birthday", "hawwy birthday"), ["p"])
Test(findBrokenKeys("starry night", "starrq light"), ["y", "n"])
Test(findBrokenKeys("beethoven", "affthoif5"), ["b", "e", "v", "n"])
Test(findBrokenKeys("mozart", "aiwgvx"), ["m", "o", "z", "a", "r", "t"])
Test(findBrokenKeys("5678", "4678"), ["5"])
Test(findBrokenKeys("!!??$$", "$$!!??"), ["!", "?", "$"])

Additional Posts

I think there's a better way to do this with either Intersect or Except, but I couldn't figure it out.

void Main()
{
	FindBrokenKeys("happy birthday", "hawwy birthday").Dump();
	FindBrokenKeys("starry night", "starrq light").Dump();
	FindBrokenKeys("beethoven", "affthoif5").Dump();
}

public IEnumerable<Char> FindBrokenKeys(string one, string two)
{
	return one.ToLower().Where((c,i) => one[i] != two[i]).Distinct();
}

enter image description here

:D

107 Characters!

findBrokenKeys = (x, y) => Array.from(x.split("")
  .reduce((a, c, i) => c != y[i] ? a.add(c) : a, new Set()))

Full w/ tests:

Test = (x, y) => x == y ? console.log("Pass", "Yours", x, "Correct:", y) || true : console.log("Fail", "Yours", x, "Correct:", y) || false;

findBrokenKeys = (x, y) => Array.from(x.split("").reduce((a, c, i) => c != y[i] ? a.add(c) : a, new Set()))

Test(findBrokenKeys("happy birthday", "hawwy birthday").join(""), ["p"].join(""))
Test(findBrokenKeys("starry night", "starrq light").join(""), ["y", "n"].join(""))
Test(findBrokenKeys("beethoven", "affthoif5").join(""), ["b", "e", "v", "n"].join(""))
Test(findBrokenKeys("mozart", "aiwgvx").join(""), ["m", "o", "z", "a", "r", "t"].join(""))
Test(findBrokenKeys("5678", "4678").join(""), ["5"].join(""))
Test(findBrokenKeys("!!??$$", "$$!!??").join(""), ["!", "?", "$"].join(""))