Z
Z
zekohina2016-10-29 00:08:37
JavaScript
zekohina, 2016-10-29 00:08:37

How to do the same but with recursion?

There is a code that returns an array of all possible combinations of 3 letters. Those. ['aaa', 'aab', 'aac', ..., 'zzy', 'zzz']

function getWords() {
    let arr = [];
    for (let i=0; i<26; i++) {
        let str = String.fromCharCode(97+i);
        for (let i=0; i<26; i++) {
            let str2 = str + String.fromCharCode(97+i);
            for (let i=0; i<26; i++) {
                arr.push(str2 + String.fromCharCode(97+i));
            }
        }
    }
    return arr
}

You need to make a getWords(size) format function that will return all possible combinations depending on the specified size. Naturally, you can’t do this with cycles, you need recursion.
Please help me write an algorithm for this function.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2016-10-29
@zekohina

codereview.stackexchange.com/questions/7001/genera...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question