-
Notifications
You must be signed in to change notification settings - Fork 0
/
500-Keyboard_Row.html
51 lines (43 loc) · 1.49 KB
/
500-Keyboard_Row.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<html>
<script>
/**
* @param {string[]} words
* @return {string[]}
*/
// var findWords = function(words) {
// let rows = [];
// const row1 = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'];
// const row2 = ['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'];
// const row3 = ['Z', 'X', 'C', 'V', 'B', 'N', 'M'];
// rows = rows.concat([row1, row2, row3]);
// // rows.push(...[row1, row2, row3]);
// // rows.push.apply(rows,[row1, row2, row3]);
// let found = [];
// for(let word of words){
// let wordToUpperCase = word.toUpperCase();
// let counts = [0, 0, 0];
// for(let i = 0; i < wordToUpperCase.length; i++){
// counts[0] += (rows[0].indexOf(wordToUpperCase.charAt(i)) === -1 ? 0 : 1);
// counts[1] += (rows[1].indexOf(wordToUpperCase.charAt(i)) === -1 ? 0 : 1);
// counts[2] += (rows[2].indexOf(wordToUpperCase.charAt(i)) === -1 ? 0 : 1);
// }
// if(counts.filter(count => count === 0).length === 2){
// found.push(word);
// }
// }
// return found;
// };
var findWords = function(words) {
return words.filter((w) => {
// remove word from array if it fails matching all three rows
if (
!/^[qwertyuiop]*$/i.test(w) &&
!/^[asdfghjkl]*$/i.test(w) &&
!/^[zxcvbnm]*$/i.test(w)
) return false;
return true;
});
};
console.log(findWords(["Hello","Alaska","Dad","Peace"]));
</script>
</html>