Skip to content

Instantly share code, notes, and snippets.

@kylelong
Created May 9, 2020 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylelong/109a19069787b1ab6d96e360f846d59c to your computer and use it in GitHub Desktop.
Save kylelong/109a19069787b1ab6d96e360f846d59c to your computer and use it in GitHub Desktop.
5/3/2020 interview problem of the week - oneRow
//Week of May 3rd 2020
//Given an array of words, return the words that can be typed using letters of only one row on a keyboard.
//QWERTY keyboard only
let rowOne = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
let rowTwo = ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l']
let rowThree = ['z', 'x', 'c', 'v', 'b', 'n', 'm']
//Looks at eache character of each word and dtermines if they are on the same row (in the same array as the first letter )
function oneRow (arr) {
words = []
for (let i = 0; i < arr.length; i++) {
let word = arr[i]
let singleRow = true
//assumes word.length > 0
let row = []
if (rowOne.includes(word[0])) {
row = rowOne
}
if (rowTwo.includes(word[0])) {
row = rowTwo
}
if (rowThree.includes(word[0])) {
row = rowThree
}
for (let j = 0; j < word.length; j++) {
if (row.includes(word[j]) == false) {
singleRow = false
break
}
}
if (singleRow) {
words.push(word)
}
}
console.log(words)
}
oneRow(['candy', 'doodle', 'pop', 'shield', 'lag', 'typewriter'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment