Previously I have questions asking about beautify js codes in this link, but this time i'm not sure how to beautify this code below because im using words instead of index numbers. (bolted words are the reason it is NOT SAME as my previous question)
Original JS Code
//food
$("#food_east").keyup(function ()
{
samefunction();
});
$("#food_west").keyup(function ()
{
samefunction();
});
$("#food_north").keyup(function ()
{
samefunction();
});
$("#food_south").keyup(function ()
{
samefunction();
});
//beverage
$("#beverage_east").keyup(function ()
{
samefunction();
});
$("#beverage_west").keyup(function ()
{
samefunction();
});
$("#beverage_north").keyup(function ()
{
samefunction();
});
$("#beverage_south").keyup(function ()
{
samefunction();
});
And this is only food and beverage, i have not include others such as candy, snacks etc because its too messy to dump all my codes here. I can only come up with this solution, is there better solution?
var compass = ["east", "west", "north", "south"];
for(var i=0; i<3; i++)
{
var name = "#food_";
name += compass[i];
$(name).keyup(function () {
samefunction();
});
var name = "#beverage_";
name += lokasi[i];
$(name).keyup(function () {
samefunction();
});
}