Skip to main content
added 441 characters in body; edited tags
Source Link

In trying to solve the following problem:

Generate all combinations of an array of string. Ex: Input: ['A', 'T', 'C', 'K'] Output: [ 'ATCK', 'ATC', 'ATK', 'AT', 'ACK', 'AC', 'AK', 'A', 'TCK', 'TC', 'TK', 'T', 'CK', 'C', 'K', '' ] 

I have the following code:

function getSpellableWords(arr) { const res = []; // helper(arr, res, 0, ''); helper(arr, res, 0, []); return res; } function helper(arr, res, i, slate) { const char = arr[i]; console.log(i, 'i') if(i === arr.length) { res.push(slate.join('')); return; } // slate = slate + char; slate.push(char) console.log(slate, 'slate') helper(arr, res, i+1, slate); slate.pop() helper(arr, res, i+1, slate); } getSpellableWords(['A', 'T', 'C', 'K']); 

My questions is:

If I remove the following lines in the code:

helper(arr, res, i+1, slate); 

Once i is equal to 5 (which is the array.length), the code will stop after it pushes slate into res. However, if I leave that line in, a call stack is set up and so i will go up from 1->5, then pop out gradually to 4 then 3 then back to 4 and so on. Why does that happen?

Clarification: So I understand that for each recursion call, another i variable is generated. However, I was expecting the second recursion call to also generate i from 1->4 again, but this time instead of incrementing it linearly, there's backtracking going on as well. Why wasn't there backtracking in the first call, and why does the second call generate the rest of the results when the first call only generates the first result?

In trying to solve the following problem:

Generate all combinations of an array of string. Ex: Input: ['A', 'T', 'C', 'K'] Output: [ 'ATCK', 'ATC', 'ATK', 'AT', 'ACK', 'AC', 'AK', 'A', 'TCK', 'TC', 'TK', 'T', 'CK', 'C', 'K', '' ] 

I have the following code:

function getSpellableWords(arr) { const res = []; // helper(arr, res, 0, ''); helper(arr, res, 0, []); return res; } function helper(arr, res, i, slate) { const char = arr[i]; console.log(i, 'i') if(i === arr.length) { res.push(slate.join('')); return; } // slate = slate + char; slate.push(char) console.log(slate, 'slate') helper(arr, res, i+1, slate); slate.pop() helper(arr, res, i+1, slate); } getSpellableWords(['A', 'T', 'C', 'K']); 

My questions is:

If I remove the following lines in the code:

helper(arr, res, i+1, slate); 

Once i is equal to 5 (which is the array.length), the code will stop after it pushes slate into res. However, if I leave that line in, a call stack is set up and so i will go up from 1->5, then pop out gradually to 4 then 3 then back to 4 and so on. Why does that happen?

In trying to solve the following problem:

Generate all combinations of an array of string. Ex: Input: ['A', 'T', 'C', 'K'] Output: [ 'ATCK', 'ATC', 'ATK', 'AT', 'ACK', 'AC', 'AK', 'A', 'TCK', 'TC', 'TK', 'T', 'CK', 'C', 'K', '' ] 

I have the following code:

function getSpellableWords(arr) { const res = []; // helper(arr, res, 0, ''); helper(arr, res, 0, []); return res; } function helper(arr, res, i, slate) { const char = arr[i]; console.log(i, 'i') if(i === arr.length) { res.push(slate.join('')); return; } // slate = slate + char; slate.push(char) console.log(slate, 'slate') helper(arr, res, i+1, slate); slate.pop() helper(arr, res, i+1, slate); } getSpellableWords(['A', 'T', 'C', 'K']); 

My questions is:

If I remove the following lines in the code:

helper(arr, res, i+1, slate); 

Once i is equal to 5 (which is the array.length), the code will stop after it pushes slate into res. However, if I leave that line in, a call stack is set up and so i will go up from 1->5, then pop out gradually to 4 then 3 then back to 4 and so on. Why does that happen?

Clarification: So I understand that for each recursion call, another i variable is generated. However, I was expecting the second recursion call to also generate i from 1->4 again, but this time instead of incrementing it linearly, there's backtracking going on as well. Why wasn't there backtracking in the first call, and why does the second call generate the rest of the results when the first call only generates the first result?

Source Link

When does the call stack get set up in Javascript?

In trying to solve the following problem:

Generate all combinations of an array of string. Ex: Input: ['A', 'T', 'C', 'K'] Output: [ 'ATCK', 'ATC', 'ATK', 'AT', 'ACK', 'AC', 'AK', 'A', 'TCK', 'TC', 'TK', 'T', 'CK', 'C', 'K', '' ] 

I have the following code:

function getSpellableWords(arr) { const res = []; // helper(arr, res, 0, ''); helper(arr, res, 0, []); return res; } function helper(arr, res, i, slate) { const char = arr[i]; console.log(i, 'i') if(i === arr.length) { res.push(slate.join('')); return; } // slate = slate + char; slate.push(char) console.log(slate, 'slate') helper(arr, res, i+1, slate); slate.pop() helper(arr, res, i+1, slate); } getSpellableWords(['A', 'T', 'C', 'K']); 

My questions is:

If I remove the following lines in the code:

helper(arr, res, i+1, slate); 

Once i is equal to 5 (which is the array.length), the code will stop after it pushes slate into res. However, if I leave that line in, a call stack is set up and so i will go up from 1->5, then pop out gradually to 4 then 3 then back to 4 and so on. Why does that happen?