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


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="" rel="nofollow"></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.

    keypad_mapings =    [

    get_all_words([1, 2, 3]);

    function get_all_words(pressed_buttons) {
        var letters = [];
        for(i=0; i&lt;pressed_buttons.length; 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);

    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

        if(current_word.length &gt;= limit) {
            if(current_word.length == limit){
            return found_words;
            //itterating button presses
            for(i=0; i&lt;letters.length; i++) {
                current_letters = letters[i];
                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...