I wrote this Javascript code.
function updateCost(plotcode) {
if (plotcode == "KKPP") {
alert("Fully Invalid!!!");
KKPP();
} else if (plotcode == 1) {
var pValue = parseInt(localStorage.plot1);
var opValue = 2 * pValue;
var mval = 760;
if (opValue >= mval) {
var opValue = mval;
}
var k = (localStorage.plot1 = opValue - 10);
} else if (plotcode == 2) {
var pValue = parseInt(localStorage.plot2);
var opValue = 2 * pValue;
var mval = 760;
if (opValue >= mval) {
var opValue = mval;
}
var k = (localStorage.plot2 = opValue - 10);
} else if (plotcode == 3) {
var pValue = parseInt(localStorage.plot3);
var opValue = 2 * pValue;
var mval = 820;
if (opValue >= mval) {
var opValue = mval;
}
var k = (localStorage.plot3 = opValue - 20);
} else {
alert("Invalid Plot Code");
KKPP();
}
alert("Rent of Plot " + plotcode + " is updated to " + k);
addtoHistory("Plot " + plotcode, "Rent Updated to " + k);
}
This code is very long.
As you can see the code is repeating in this format
} else if (plotcode == 7) {
var pValue = parseInt(localStorage.plot7);
var opValue = 2 * pValue;
var mval = 940;
if (opValue >= mval) {
var opValue = mval;
}
var k = (localStorage.plot7 = opValue - 40);
again and again
How can I put this inside a loop, or is there anything else that I can do to make this short?