0

I have been trying to recreate the JavaScript version of this C program .
The program is suppose to generate all possible combinations from the characters in the array set. Basically there are two functions printAllKeyLength and printAllKeyLengthR.Each time the second function is called , it checks for k variable which upon equal to 0 prints the result. The for loop within it iterates through length of the character set and appends each char to a variable prefix and variable new prefix. After that it calls itself repeatedly until each variant of it results in the k variable of it equal to zero. The C program works fine but the JavaScript completes only through the last few calls of the function which results in incomplete result. The rest previous calls are omitted.I tried changing the JavaScript functions to async function but didn't work out.Can any one explain why this is happening and how to fix it without changing the functions and structure of the program entirely ?


[C Language] --- This works fine but not the JavaScript Version { below }
#include <stdio.h>
#include <string.h>

int c = 0, chct;
char subs[10];
char set0[27] = "";

void
printallkeylengR(char set[], char prefix[], int n, int k)
{

    if (k == 0) {
        printf("%s\n", prefix);
    }
    else {
        for (int i = 0; i < n; i++) {
            char newprefix[10] = "";

            char tmp[2] = "";

            tmp[0] = set[i];
            strcpy(subs, prefix);
            strcat(prefix, tmp);
            strcpy(newprefix, prefix);
            strcpy(prefix, subs);

            printallkeylengR(set, newprefix, n, k - 1);
        }
    }
}

void
printallkeyleng(char set[], int k, int n)
{

    char ch[1] = "";

    printallkeylengR(set, ch, n, k);
}

int
main()
{

    chct = 0;
    for (int i = 65; i <= 90; i++) {
        set0[chct] = i;
        chct += 1;
    }
    set0[27] = '\0';

    printallkeyleng(set0, 4, strlen(set0));

    return 0;
}

[Javascript] --- Does not iterate through all the results !!!
function printAllKeyLengthR(set,prefix,n,k){
    if(k==0){
        document.body.appendChild(document.createTextNode(prefix+" "));
        }

    else{
    console.log("Prefix "+prefix+" K= "+k)

    for(i=0;i<n;i++){
        newprefix=prefix.toString()+set[i].toString();
    console.log("New Prefix "+newprefix+" K= "+k+" i= "+i);console.log("   ");
        const x = printAllKeyLengthR(set,newprefix,n,k-1)
        
    }
    
}
}

function printAllKeyLength(set,k,n){
    printAllKeyLengthR(set,"",n,k);
}

set=["0","1","2","3","4","5","6","7","8","9"];

//printAllKeyLength(set ,final_length,no_of_elem)
printAllKeyLength(set,4,10)
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • Javascript will timeout a script that goes on for too long. Well, not really. The "browser" will timeout the script. – GetSet Dec 17 '20 at 20:48
  • So , Is there a way to work around this ? And how long is the time out value ?? – Phuntsog Wangdus Dec 17 '20 at 20:54
  • In C, doing `set0[27] = '\0';` goes one beyond the end of the array. This is UB (undefined behavior). If compiled with `-Wall` _and_ `-O2`, `gcc` flags this. So, I'd change the declaration to: `char set0[27 + 1];` or `char set0[28];` Actually, I'd do: `#define MAX 28` and then `char set0[MAX];` and: `set0[MAX - 1] = 0;` – Craig Estey Dec 17 '20 at 21:00
  • 2
    In the js version, `for (i = 0; ...)` should be `for (let i = 0; ...)`, otherwise you will create a global variable `i` that will be used across all recursions. – M Oehm Dec 17 '20 at 21:10
  • `ch` is only large enough to hold a single character (as a C style string that means just the null terminator!), but the function writes up to `k+1` characters to it. More undefined behavior! – Kevin Dec 17 '20 at 21:16
  • @MOehm : Thanks a lotttttt !!! . I have been into this problem for many hours... Finally ! your solution solved it . Such a relief after I checked into my Chrome inspector.. CraigEstey and Kevin .. Thanks for pointing out the mistake... I wrote C program in a hurry. – Phuntsog Wangdus Dec 17 '20 at 21:42

1 Answers1

0

The issue with your JavaScript implementation is that the for loop is not waiting for the recursive call to finish before moving on to the next iteration. As a result, the next iteration can start before the previous one has finished, leading to incomplete results.