0

I have product codes like: "ABC123", "ABC 8765", "ABC 98761", "ABC89", "TRM 4576", "TRM IKQ", TRM-238", "ERW 78345", "ERW BRB", "ERW 877a" etc etc.

I need JavaScript to return a brand based on three first letters of the product codes. If I include all the product codes on to the JS code, it will become a really long one. What I have done until now, and it is working but:

QUOTE EXAMPLE:

    if (PO == "ABC 98761"){
    return "Heinisch";
    else if (PO == "ABC 87NN"){
       return "Heinisch"; 
    else if (PO == "ABC TT5"){
       return "Heinisch";
    else if (PO == "ABC 6334T"){
       return "Heinisch";    
    else if (PO == "ABC 3543") {
       return "Heinisch ";
    else if (PO == "RRR J98777"){
       return "Reinhart";
    else {
       return "not known";

UNQUOTE How could I shorten the code so that variable PO starting with ABC would return "Heinisch"? All the product codes starting wit RRR would return Reinhart, etc.

I want my code to be cleaner... Any advise pls?

F88
  • 1
  • Use a regex (regular expression)? Grab the first three characters and use those? – Dave Newton Aug 30 '22 at 15:18
  • Is the product code always seprated by a space "ABC 12222" from the rest of the PO? – user1514042 Aug 30 '22 at 15:18
  • @user1514042 Not according to the question text. – Dave Newton Aug 30 '22 at 15:18
  • I haven't asked you bud, also using Regex for this is a totally inadequate:) – user1514042 Aug 30 '22 at 15:20
  • Well, "bud", SO is a public forum, and when a comment asks a question that's answered by reading the question, I'd expect someone to re-answer it for clarity. – Dave Newton Aug 30 '22 at 15:22
  • 1
    @DaveNewton regular expression for *extremely basic string operation*?! – VLAZ Aug 30 '22 at 15:24
  • `const first3 = PO.substring(0, 3);//outputs first 3 letters` then you can do a switch with `first3` where you return `'Heinisch'` if `first3` is equal to `'ABC'` and return `'Reinhart'` if `first3` is equal to `'RRR'` and your default can return `'not known'` – Chris G Aug 30 '22 at 15:27
  • @VLAZ *shrug* Questions get options; poorly-specified questions tend towards more options. – Dave Newton Aug 30 '22 at 15:27
  • @DaveNewton and poor suggestions for regex tend to lead to even more and poorer questions. It's an endless cycle. – VLAZ Aug 30 '22 at 15:28
  • @VLAZ And the wheel turns. I'd think the most *effective* way to handle what may be a bad suggestion is to address the *OP* and say *why* it's a bad suggestion so they're educated and/or not mislead--but some folks are less motivated by helping the OP than other motivations *shrug* – Dave Newton Aug 30 '22 at 15:34
  • @DaveNewton so, to recap - your job is to provide exceptionally bad solutions as comments, and others should then go and spend their own time to explain exactly why it's bad. In comments. For questions which already have better answers elsewhere. While you sit here explaining that it's good to explain so OP understands but it's not your job to educate. Have you considered a) educating the OP yourself if that's ultimately what you're after. b) not actually giving bad suggestions. I'm done here. I don't appreciate snide suggestions that I should be wasting my time. – VLAZ Aug 30 '22 at 15:43
  • @VLAZ (1) It's not an *exceptionally* bad solution, it's a somewhat inefficient solution if the question is completely accurate. (2) You spent a lot of words pretending something horrible has happened here--you're the one choosing to waste your time, but it's your time to waste, so have at it. – Dave Newton Aug 30 '22 at 15:47
  • Thanks for taking time. There is not always a space after the Product code.I'm a beginner in JS and did my best to explain the challenge I have. – F88 Sep 01 '22 at 07:22

0 Answers0