Keyboard Row LeetCode solution in JavaScript
1var findWords = function (words) {2const firstRow = {3q: true,4w: true,5e: true,6r: true,7t: true,8y: true,9u: true,10i: true,11o: true,12p: true,13};14const secondRow = {15a: true,16s: true,17d: true,18f: true,19g: true,20h: true,21j: true,22k: true,23l: true,24};25const thirdRow = {26z: true,27x: true,28c: true,29v: true,30b: true,31n: true,32m: true,33};34let result = [];3536const helperFunc = (word) => {37let targetRow;38//Determine in which row the word should be;39if (firstRow[word[0].toLowerCase()]) targetRow = firstRow;40if (secondRow[word[0].toLowerCase()]) targetRow = secondRow;41if (thirdRow[word[0].toLowerCase()]) targetRow = thirdRow;4243for (let i = 1; i < word.length; i++) {44if (!targetRow[word[i].toLowerCase()]) {45return false;46}47}48return true;49};5051for (let i = 0; i < words.length; i++) {52if (helperFunc(words[i])) {53result.push(words[i]);54}55}56return result;57};