generate all possible words typed on a phone keypad recursively in javascript

admin

Administrator
Staff member
generate all possible words typed on a phone keypad in javascript,

assume you have access to an api to identify correct words.

EDIT: my memory is very bad so appoligies but I can't remember the exact phrasing, the problem is the same as here:
<a href="http://tianrunhe.wordpress.com/2012/07/27/letter-combinations-of-a-phone-number/" rel="nofollow">http://tianrunhe.wordpress.com/2012/07/27/letter-combinations-of-a-phone-number/</a>

I had a technical test for a job interview recently but was hit by the above question and I think I've failed as recursion is something I've never wrapped my head around. The question assumes I have access to an api to identify correct words, I have not coded in this part as it's something I can easily add when this works.

I want to understand where I went wrong so I have a better chance next time. The code I have so far is:

His answer is in java though, not javascript. I have used this function for a reference but can't get it to work yet.

Code:
    keypad_mapings =    [
                                [],
                                ['a','b','c'],
                                ['d','e','f'],
                                ['g','h','i'],
                                ['j','k','l'],
                                ['m','n','o'],
                                ['p','q','r','s'],
                                ['t','u','v'],
                                ['w','x','y','z']
                        ];

    get_all_words([1, 2, 3]);


    function get_all_words(pressed_buttons) {
        var letters = [];
        for(i=0; i&lt;pressed_buttons.length; i++) {
            letters.push(keypad_mapings[pressed_buttons[i]]);
        }

        var progress = 0;
        current_word = '';
        limit = pressed_buttons.length;
        found_words = [];

        var return_value = recursive_word_generator(letters, progress, current_word, limit, found_words);
        console.log(return_value);
    }


    function recursive_word_generator(letters, progress, current_word, limit, found_words) {

        // I expect this to only be zero in first iteration, all recursive calls should be incremented to some degree
        console.log(progress);

        if(current_word.length &gt;= limit) {
            if(current_word.length == limit){
                found_words.push(current_word);
            }
            return found_words;
        }else{
            //itterating button presses
            for(i=0; i&lt;letters.length; i++) {
                current_letters = letters[i];
                console.log(current_letters);
                for(i = progress; i&lt;letters.length; i++) {
                    current_word = current_word.concat(current_letters[i]);
                    found_words = recursive_word_generator(letters.slice(1), i, current_word, limit, found_words);
                }
            }
        }
        return found_words
    }

EDIT: I am now getting the first 3 results correctly, however the recursion doesn't seem to be working, I would expect the progress variable to increment inside the recursion but when I log it its value is always 0. I feel like I'm so close to a correct answer...