Filter an Array by Keyboard Rows

I subscribe to Cassidoo’s newsletter, and in it, she offers up a coding challenge. Lately, I’ve been using it as a way to experiment with ChatGPT. Just to be sure, I do not submit these for credit. It’s just a way to learn how to approach it both from the code and the prompting.

Given an array of words, return the words that can be typed using letters of only one row on a keyboard. For bonus points, include the option for a user to pick the type of keyboard they are using (ANSI, ISO, etc), and/or give options for how many/which rows are allowed!

oneRow(['candy', 'fart', 'pop', 'Zelda', 'flag', 'typewriter'])
['pop', 'flag', 'typewriter']
— Cassidoo

The first attempt was a miss, but it helps me learn how to be more direct about my prompts. I used:

Write a javascript function that will filter an array down to words that can be typed by using a single row on a keyboard, allowing for the type of keyboard to be chosen. An example array is ['candy', 'fart', 'pop', 'zelda', 'flag']

I won’t output the full thing, but it led with, “Here’s an implementation of the function you’re asking for. In this version, you’ll specify the keyboard row as a string of characters, and it will filter the array accordingly.”, along with a walkthrough of the code for that.

I followed up with, “Refactor this to allow for the keyboard type to be an argument instead of row”, which worked. In the future, I know that I should specify the arguments.

Sure, we can adjust the function to allow for keyboard type as an argument instead of individual rows. Here's an implementation for three keyboard types: QWERTY, Dvorak, and AZERTY.

function filterByKeyboardRow(words, keyboardType) {
    // Define the rows for different keyboard types
    const keyboardRows = {
        'QWERTY': ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'],
        'Dvorak': ['pyfgcrl', 'aoeuidhtns', 'qjkxbmwvz'],
        'AZERTY': ['azertyuiop', 'qsdfghjklm', 'wxcvbn']
// Check that we have rows for the provided keyboard type
if (!keyboardRows[keyboardType]) {
    throw new Error(`Unknown keyboard type: ${keyboardType}`);

// Convert the rows to Sets for easier lookup
const rowSets = keyboardRows[keyboardType].map(row => new Set(row.split('')));

// Filter the words
const result = words.filter(word => {
    // Convert word to lower case and check every character
    const lowerCaseWord = word.toLowerCase();
    // Check if word can be typed using any row of the keyboard
    return rowSets.some(rowSet => [...lowerCaseWord].every(char => rowSet.has(char)));

return result;


Now you can filter words based on the type of keyboard:

const words = ['candy', 'fart', 'pop', 'zelda', 'flag'];

console.log(filterByKeyboardRow(words, ‘QWERTY’)); // [‘pop’, ‘fart’, ‘flag’] console.log(filterByKeyboardRow(words, ‘Dvorak’)); // [‘pop’, ‘zelda’] console.log(filterByKeyboardRow(words, ‘AZERTY’)); // [‘pop’, ‘zelda’]

This code assumes that any word is typed using only one row of the keyboard. If a word uses letters from different rows, it will be excluded. If the keyboard type isn't recognized, it will throw an error.

— ChatGPT 4

Ooh, before publishing this I found out I have access to shared links, so you can see the actual chat.

Check out
Front-end conf
we're bringing the conf back in April 2024!
10 speakers across two days, with plenty
of time for conversations