...

View Full Version : change var a to var b if var a greater ??



TYOC
01-27-2012, 05:25 PM
I've run into a wall with this and would really appreciate some assistance. I need to calculate the value of an insurance premium based on two user input variables. If the first variable is greater than the second variable, the first variable needs to be equal to the second variable to complete the calculation. For example, if currcoverage = 100000 and newcoverage = 80000 then currcoverage= 80000.

I'm also aware eval is bad, but I donít know another way to do it. I'm really trying to learn this stuff. Thank you so much.

Here's what I have so far.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form name="formCalc" action="#">
Current Coverage<input type="text" name="currentcoverage" />
<br />
New Coverage Amount<input name="newcoverage" /><br />
<br />
<input onclick="calculateValues()" value="Calculate" type="button" name="Calculate" /><br />
<br />
Rate<input type="text" name="standardrate"/><br />
Rate with credit for current coverage<input name="ratewithcoverage" type="text" readonly /><br />
</form>
<script type="text/javascript">
function resetInputs() {}
function RoundUpToThousand(passParm) {
var resultThou = Math.floor((eval(passParm) + 999.99) * 0.001) * 1000;
return resultThou;
}
function roundCent(passParm) {
var resultCent = Math.floor((eval(passParm) + 0.005) * 100) * 0.01;
return resultCent;
}
function formatDecimal(passParm, passPrecision) {
var j1;
var strMtx = [];
var resultDecimal;
var strMid;
var strFin;
passParm = passParm * 1;
strMid = "" + Math.round(eval(passParm) * Math.pow(10, passPrecision));
while (strMid.length <= passPrecision) {
strMid = "0" + strMid;
}
var decPos = strMid.length - passPrecision;
strFin = strMid.substring(0, decPos);
j1 = Math.ceil(strFin.length / 3) - 1;
while (strFin.length > 0) {
strMtx[j1] = strFin.substring(strFin.length - 3, strFin.length);
strFin = strFin.substring(0, strFin.length - 3);
j1--;
}
resultDecimal = strMtx.join(",") + "." + strMid.substring(decPos, strMid.length);
return resultDecimal;
}
function formatDollar(passParm) {
var decPlaces = 2;
var formattedVal = "$" + formatDecimal(passParm, decPlaces);
return formattedVal;
}
function calc1Value(InsuredValue, aB1, aB2, aB3, aB4, aB5, aB6, pR1, pR2, pR3, pR4, pR5, pR6)
{
var premRate = 0;
var cumPrem = 119;
var thou = RoundUpToThousand(InsuredValue) / 1000;
var remainThou = thou;
var breakThou = RoundUpToThousand(aB1) / 1000;
if (remainThou > breakThou) {
premRate = eval(pR1);
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB2 - aB1) / 1000;
premRate = eval(pR2);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB3 - aB2) / 1000;
premRate = eval(pR3);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB4 - aB3) / 1000;
premRate = eval(pR4);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
breakThou = RoundUpToThousand(aB5 - aB4) / 1000;
premRate = eval(pR5);
if (remainThou > breakThou) {
cumPrem = cumPrem + premRate * breakThou;
remainThou = remainThou - breakThou;
premRate = eval(pR6);
cumPrem = cumPrem + premRate * remainThou;
}
else {
premRate = eval(pR5);
cumPrem = cumPrem + premRate * remainThou;
}
}

premRate = eval(pR4);
cumPrem = cumPrem + premRate * remainThou;
}
else {
premRate = eval(pR3);
cumPrem = cumPrem + premRate * remainThou;
}
}
else {
premRate = eval(pR2);
cumPrem = cumPrem + premRate * remainThou;
}
}
else {
premRate = eval(pR1);
cumPrem = cumPrem + premRate * remainThou;
}
cumPrem = roundCent(cumPrem);
result = cumPrem;
return result;
}
function conditionInput(passParm) {
if (passParm === "") {
result = 0;
}
else {
if (passParm.indexOf("$") > -1) {
passParm = passParm.substring(1);
}
while (passParm.indexOf(",") > -1) {
tempStr = passParm;
passParm = tempStr.substring(0, tempStr.indexOf(",")) + tempStr.substring(tempStr.indexOf(",") + 1);
}
result = passParm;
}
return result;
}
function calculateValues() {
var currentcoverage = conditionInput(document.formCalc.currentcoverage.value);
var newcoverage = conditionInput(document.formCalc.newcoverage.value);
amtBreak1 = 100000;
amtBreak2 = 1000000;
amtBreak3 = 5000000;
amtBreak4 = 10000000;
amtBreak5 = 15000000;
amtBreak6 = 15000000.01;
premRate1 = 6;
premRate2 = 2.75;
premRate3 = 2;
premRate4 = 1.50;
premRate5 = 1.25;
premRate6 = 1;
if (isNaN(currentcoverage)) {
alert("Invalid Coverage Amount");
document.formCalc.currentcoverage.value = "";
document.formCalc.currentcoverage.focus();
return (false);
}
if (currentcoverage === 0) {
alert("Please enter the Current Coverage.");
document.formCalc.currentcoverage.focus();
return false;
}

// I DON'T KNOW WHAT TO DO HERE
// if (currentcoverage > newcoverage) {
// document.formCalc.currentcoverage.value = document.formCalc.newcoverage.value;
// return false;
// }
if (isNaN(newcoverage)) {
alert("Invalid Loan Amount");
document.formCalc.newcoverage.value = "";
document.formCalc.newcoverage.focus();
return (false);
}
if (newcoverage === 0) {
alert("Please enter the Loan Amount!");
document.formCalc.newcoverage.focus();
return false;
}
ratewithcoverage = calc1Value(newcoverage, amtBreak1, amtBreak2, amtBreak3, amtBreak4, amtBreak5, amtBreak6, premRate1, premRate2, premRate3, premRate4, premRate5, premRate6);
ratewithcoverage = roundCent(ratewithcoverage);
standardrate = ratewithcoverage - calc1Value(currentcoverage, amtBreak1, amtBreak2, amtBreak3, amtBreak4, amtBreak5, amtBreak6, premRate1, premRate2, premRate3, premRate4, premRate5, premRate6) * 0.30;
titlePolicy = ratewithcoverage;
document.formCalc.currentcoverage.value = formatDollar(currentcoverage);
document.formCalc.newcoverage.value = formatDollar(newcoverage);
document.formCalc.ratewithcoverage.value = formatDollar(ratewithcoverage);
document.formCalc.standardrate.value = formatDollar(standardrate);
return(true);
}

</script>
</body>
</html>

xelawho
01-27-2012, 05:38 PM
I may be missing something here, but aren't you just looking for:



if (currentcoverage > newcoverage) {
currentcoverage= newcoverage
document.formCalc.currentcoverage.value = document.formCalc.newcoverage.value;
alert(currentcoverage)
// return false;
}

Philip M
01-27-2012, 05:39 PM
Current Coverage <input type = "text" id = "currcov">
<br>
New Coverage <input type = "text" id = "newcov" onblur = "checkIt()">
<br>

<script type = "text/javascript">
function checkIt() {
var first = Number(document.getElementById("currcov").value || 0);
var second = Number(document.getElementById("newcov").value || 0);

if (first >= second) {
first = second
document.getElementById("currcov").value=first;
}

}
</script>

eval is indeed strongly disapproved of. Why are you using it?


"The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest -- but the myth -- persistent, persuasive and unrealistic." - John F Kennedy

TYOC
01-27-2012, 06:09 PM
No my friend. I was the one missing something and it was right in front of my face. Thank you for pointing it out to me. I really appreciate it. That works!

TYOC
01-27-2012, 06:21 PM
Philip M. you are certainly a Master Coder. Your suggestion works as well.

As stated in my original post, eval is the only way I know to do it. I'm obviously a rank amateur. I assume the fix would be a question for another thread.

I can't tell you how much I appreciate your assistance. Thank you very much.

felgall
01-27-2012, 08:31 PM
As stated in my original post, eval is the only way I know to do it.

One thing you need to remember then is that eval is NEVER the way to do anything in JavaScript. There is always a better alternative and if you don't know what the right way to do something is in order to avoid using it then you should ask.

TYOC
01-27-2012, 08:44 PM
Could you help me clean it up fellgall? I certainly don't want to put unacceptable code out there. It's not a homework assignment - only for local intranet use. I'm humbled by the knowledge you expert coders possess.

felgall
01-27-2012, 09:39 PM
Philip M already posted a version of that code showing you a way to do it without using eval.

If you have some other piece of code currently using eval and want to know how to write it without the eval then simply post it to the forum and someone will be able to give you a better alternative.

xelawho
01-27-2012, 10:27 PM
I think he's talking about this bit:



function RoundUpToThousand(passParm) {
var resultThou = Math.floor((eval(passParm) + 999.99) * 0.001) * 1000;
return resultThou;
}
function roundCent(passParm) {
var resultCent = Math.floor((eval(passParm) + 0.005) * 100) * 0.01;
return resultCent;
}

which seems to me to work the same doing this:


function RoundUpToThousand(passParm) {
var resultThou = Math.floor((Number(passParm) + 999.99) * 0.001) * 1000;
return resultThou;
}
function roundCent(passParm) {
var resultCent = Math.floor((Number(passParm) + 0.005) * 100) * 0.01;
return resultCent;
}


in fact, it seems to me that all of the eval's can be replaced with Number, which is certainly a much better way to go

felgall
01-27-2012, 10:57 PM
Number(passParm) can be shortened to (+passParm) and still does exactly the same thing.

Philip M
01-28-2012, 08:10 AM
in fact, it seems to me that all of the eval's can be replaced with Number, which is certainly a much better way to go

Yes, that it what I intended to convey.

TYOC
01-29-2012, 02:58 PM
I couldnít see the forest for the trees guys.

xelawho
Philip M
felgall

Thank you! Thank you! Thank you! Each of you saved me from another day of staring at code until my eyes bleed. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum