Dropdown list of 8 items

Posted on

Problem

I have an input which contains a dropdown list of 8 items. Depending on the option the user picks, I want to change the value of their input into a a different string value. In order to do this, I am using a ton of if else statements, which make this look very bulky and I would like to condense this if at all possible. I have the following code:

if (inputFive == "Corporation"){
inputFive = "534"
}else if (inputFive == "LLC"){
inputFive = "535"
}else if(inputFive == "LLP"){
inputFive = "536"
}else if(inputFive == "Partnership"){
inputFive = "537"
}else if(inputFive == "Sole Proprietorship"){
inputFive = "538"
}else if(inputFive == "Limited Partnership"){
inputFive = "539"
} else {
inputFive = "540"
}

As you can see, this looks a little old-school, and I would like to see if there is a better/simpler way to make this happen. Just looking to condense this code if at all possible. All options/hints will be appreciated!

Solution

I am not a Javascript programmer, so I will try my best how to explain to use a dictionary. A dictionary means you can add a value to a key

Example from dotnet perls

# Creations of a dictionary
var dictionary = {"Corporation":534,"LLC":535,"LLP":536,"Partnership":537,"Sole Proprietorship":538,"Limited Partnership":539};

# Getting values of dictionary
new_inputFive = dictionary[inputfive]

# For instance when inputfive="LLC" new_inputFive = 535

Leave a Reply

Your email address will not be published. Required fields are marked *