Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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)) {
    		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>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    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;
    		alert(currentcoverage)
    //		return false;
    	}

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by TYOC View Post
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    I think he's talking about this bit:

    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

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Number(passParm) can be shortened to (+passParm) and still does exactly the same thing.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by xelawho View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    New Coder
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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
    •