-3

I'm currently working on a Node JS/Puppeteer project which inputs characters on a registration form from a dictionary that changes with a variable. For this I made a big chunk of code:

        if (c1 == 1){
            "a" = dict["Chara1"]
        } else {
            if (c1 == 2) {
                "b" = dict["Chara1"]
            } else {
                if (c1 == 3){
                    "c" = dict["Chara1"]
                } else {
                    if (c1 == 4) {
                        "d" = dict["Chara1"]
                    } else {
                        if (c1 == 5){
                            "e" = dict["Chara1"]
                        } else {
                            if (c1 == 6) {
                                "f" = dict["Chara1"]
                            } else {
                                if (c1 == 7){
                                    "g" = dict["Chara1"]
                                } else {
                                    if (c1 == 8) {
                                        "h" = dict["Chara1"]
                                    } else {
                                        if (c1 == 9){
                                            "i" = dict["Chara1"]
                                        } else {
                                            if (c1 == 10) {
                                                "j" = dict["Chara1"]
                                            } else {
                                                if (c1 == 11){
                                                    "k" = dict["Chara1"]
                                                } else {
                                                    if (c1 == 12) {
                                                        "l" = dict["Chara1"]
                                                    } else {
                                                        if (c1 == 13){
                                                            "m" = dict["Chara1"]
                                                        } else {
                                                            if (c1 == 14) {
                                                                "n" = dict["Chara1"]
                                                            } else {
                                                                if (c1 == 15){
                                                                    "o" = dict["Chara1"]
                                                                } else {
                                                                    if (c1 == 16) {
                                                                        "p" = dict["Chara1"]
                                                                    } else {
                                                                        if (c1 == 17){
                                                                            "q" = dict["Chara1"]
                                                                        } else {
                                                                            if (c1 == 18) {
                                                                                "r" = dict["Chara1"]
                                                                            } else {
                                                                                if (c1 == 19){
                                                                                    "s" = dict["Chara1"]
                                                                                } else {
                                                                                    if (c1 == 20) {
                                                                                        "t" = dict["Chara1"]
                                                                                    } else {
                                                                                        if (c1 == 21){
                                                                                            "u" = dict["Chara1"]
                                                                                        } else {
                                                                                            if (c1 == 22) {
                                                                                                "v" = dict["Chara1"]
                                                                                            } else {
                                                                                                if (c1 == 23){
                                                                                                    "w" = dict["Chara1"]
                                                                                                } else {
                                                                                                    if (c1 == 24) {
                                                                                                        "x" = dict["Chara1"]
                                                                                                    } else {
                                                                                                        if (c1 == 25){
                                                                                                            "y" = dict["Chara1"]
                                                                                                        } else {
                                                                                                            if (c1 == 26) {
                                                                                                                "z" = dict["Chara1"]
                                                                                                            } else {
                                                                                                                
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

As I'm a beginner in Node JS I've tried to search tutorials or documentation, but I didn't find/understand anything and I know that it's probably gonna end with a Node JS crash or something that doesn't work as this chunk of code will be in 8 copy. I want to know how to make it smaller.

Ps: Sorry if my English is bad, I'm not Anglophone.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
5BNeumann
  • 3
  • 2
  • 3
    That code won't work in the first place; you can't assign to a string literal. – AKX Apr 27 '23 at 11:39

1 Answers1

1

If the end result you want is the dict object having a Chara1 key with an array of alphabet (1-indexed), all you need is

const dict = {
  Chara1: [null, ..."abcdefghijklmnopqrstuvwxyz"],  // alphabet, one-indexed
  Digit1: [..."0123456789"],  // digits, zero-indexed
}

– this uses the ... spread syntax to "paste" the string into the array.

You can then do e.g. this to get the fifth (1-indexed) letter in the alphabet:

> console.log(dict.Chara1[5]);
'e'
AKX
  • 152,115
  • 15
  • 115
  • 172
  • If I use this method, can I also have a Chara2, Chara3, etc... doing the same ? – 5BNeumann Apr 27 '23 at 12:07
  • Of course. Just add them there in the dict; see my edit. – AKX Apr 27 '23 at 12:07
  • Also, in my loop I've put a system that increment c1 and then put it to 1 to increment c2 etc... As it's the same problem (I don't know how to make it smaller, if it's possible and if it could work) I asked it here. Here is the base block of this "system" '''if (c1 > 25) { c1 = 1 (same for c2, c3, etc...) } else { c1 += 1 }''' – 5BNeumann Apr 27 '23 at 13:49
  • Are you maybe trying to generate all combinations of a string? Like `aaa`, `aab`, `aac`..? – AKX Apr 27 '23 at 15:03
  • Yes that's it, I want to generate all combination of a string of 8 character/digit – 5BNeumann Apr 27 '23 at 15:20
  • Then please see my answer from 11 years ago: https://stackoverflow.com/a/8823066/51685 – AKX Apr 27 '23 at 16:39
  • I tried it and it actually broke the copy of the program on wich I make test... – 5BNeumann Apr 29 '23 at 18:17