0

I'm trying to use a switch statement to convert some shortened tokens into full words, last time I did it, it worked, this time not so much. I think it's something wrong with the types, but I have no idea how to fix it. Weirdly enough, the modifier portion works correctly, but not the source part.

function keyToSource(key)
{
    let fullSource, source, modifier;
    if(key.includes("-"))
    {
        modifier = key.substring(key.indexOf("-") + 1, key.length);
        source = key.substring(0, key.indexOf("-"));
    }
    else source = key;

    switch(source)
    {
        case "Bo": fullSource = "Body"; break;
        case "Ca": fullSource = "Capture"; break;
        case "FA": fullSource = "Forearms"; break;
        case "HL": fullSource = "Hindlegs"; break;
        case "HS": fullSource = "Hard Shell"; break;
        case "IR": fullSource = "Investigation Reward"; break;
        case "PB": fullSource = "Palico Bonus"; break;
        case "Pl": fullSource = "Plunderblade"; break;
        case "SD": fullSource = "Shiny Drop"; break;
        case "Ta": fullSource = "Tail"; break;
        case "Tr": fullSource = "Track"; break;
        default: fullSource = "Error"; break;
    }

    if(typeof modifier !== 'undefined')
    {
        switch(modifier)
        {
            case "C": fullSource += " carve"; break;
            case "G": fullSource += "(Gold)"; break;
            case "S": fullSource += "(Silver)"; break;
            case "W": fullSource += " wound"; break;
            default: fullSource = "Error" + fullSource; break;
        }
    }
    return fullSource;
}


console.log(keyToSource("Ta"));
console.log(keyToSource("Ta-C"));
Keith
  • 22,005
  • 2
  • 27
  • 44
  • Can you give us examples of `key`? – ibrahim mahrir Jul 22 '18 at 22:50
  • @ibrahimmahrir For example, "Ta-C" would return "Tail carve", while "Ta" alone would return just "Tail". The idea is to allow base words that can be modified with adding a "-" followed by letters. – J. Sanchez Jul 22 '18 at 22:54
  • 2
    "Ta-C" and "Ta" works fine. What's not working? – ggorlen Jul 22 '18 at 22:56
  • I might have to edit the post, but the way that the bigger picture version works is that I have a large raw string that is separated by new line characters. I split that large string into an array using the new lines as delimiters. The entries of this array are also strings, but these strings can be split using spaces as delimiters. So I split the string using spaces, to give each entry in the array its own array, so I end up with a two dimensional array. I might have posted with too little context. But I was worried that if I put everything it would be really difficult to answer. – J. Sanchez Jul 22 '18 at 23:04
  • Are working on Windows or Linux? – ibrahim mahrir Jul 22 '18 at 23:09
  • Indeed, don't just put everything in. Instead, learn how to create an [mcve] - so we can actually see / test the specific situation. – random_user_name Jul 22 '18 at 23:09
  • I'm working on Windows. – J. Sanchez Jul 22 '18 at 23:09
  • With Windows you may find includes doesn't work with the ie browser. – QuentinUK Jul 22 '18 at 23:13
  • @J.Sanchez Then the problem is most likely the newline delimeter you use for spliting the large string. [**On Windows the line break is `'\r\n'` not just `'\n'`**](https://stackoverflow.com/questions/7013034/does-windows-carriage-return-r-n-consist-of-two-characters-or-one-character). – ibrahim mahrir Jul 22 '18 at 23:15
  • ... also try `trim`ming the strings before passing them to `keyToSource`. – ibrahim mahrir Jul 22 '18 at 23:16
  • I figured out the problem and it ended up only being that I was feeding the function incorrect keys, what should I do? Do I delete the post, or edit it and mark solved? I'm asking because the problem was not really directly related to the question. – J. Sanchez Jul 22 '18 at 23:44

1 Answers1

1

Your code appears to work. However, you can clean up the logic quite a lot by moving your switch statements into objects:

function keyToSource(key) {
  const k = key.split("-");
  
  const source = {
    "Bo": "Body",
    "Ca": "Capture",
    "FA": "Forearms",
    "HL": "Hindlegs",
    "HS": "Hard Shell",
    "IR": "Investigation Reward",
    "PB": "Palico Bonus",
    "Pl": "Plunderblade",
    "SD": "Shiny Drop",
    "Ta": "Tail",
    "Tr": "Track"
  };
  
  const modifier = {
    "C": " carve",
    "G": "(Gold)",
    "S": "(Silver)",
    "W": " wound"
  };
  
  return (source[k[0]] || "") + (modifier[k[1]] || "");
}

console.log(keyToSource("Ta"));
console.log(keyToSource("Ta-C"));
console.log(keyToSource("PB"));
console.log(keyToSource("Ta-G"));
console.log(keyToSource("SD-W"));
console.log(keyToSource("HS-C"));
console.log(keyToSource("as- da-sdf")); // test invalid entry

Feel free to post a clarification if this isn't cutting it for you.

ggorlen
  • 44,755
  • 7
  • 76
  • 106