Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: change var a to var b if var a greater ??

1. ## change var a to var b if var a greater ??

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.

Code:
``` <!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)) {
document.formCalc.currentcoverage.value = "";
document.formCalc.currentcoverage.focus();
return (false);
}
if (currentcoverage === 0) {
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)) {
document.formCalc.newcoverage.value = "";
document.formCalc.newcoverage.focus();
return (false);
}
if (newcoverage === 0) {
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>```

• I may be missing something here, but aren't you just looking for:

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

• Code:
```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

• 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!

• 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.

• Originally Posted by TYOC
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.

• 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.

• 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.

Code:
```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:
Code:
```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

• Number(passParm) can be shortened to (+passParm) and still does exactly the same thing.

• Originally Posted by xelawho
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.

• 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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•