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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to code a price generator with the use of hours

    Hello, I am very very new to Javascript, so much so that I have absolutely no idea how to go about what I am wanting. I have a website I am creating in which there is a page that shows the viewer different rates per hour. I am trying to create a quote generator in which they input the price per hour and then the number of hours they want to use the item. Then after submitting these fields I want them to see the price ti will cost them. This is some script that I have found and been trying to manipulate into what I am wanting but I am definitely not doing it right. Any suggestions on what I need to fix or do would be extremely helpful. Also feel free to tell me that I am going in the wrong direction with this script as well.


    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Calc</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">
    <!--
    $(document).ready(function() {
    $('#calculateTotal').click(function() {

    var shirtsPrice = 10.50, perColorPrice = 5.99;

    var inputNum_of_Shirts = $('#shirts').val();
    var inputNum_of_FColor = $('#colorsOnFront').val();
    var inputNum_of_BColor = $('#colorsOnBack').val();

    var totalCost = (inputNum_of_Shirts*parseFloat(shirtsPrice))
    +(parseFloat(perColorPrice)*inputNum_of_FColor)
    +(parseFloat(perColorPrice)* inputNum_of_BColor );
    var perShirtCost = (parseFloat(shirtsPrice))
    +(parseFloat(perColorPrice)*inputNum_of_FColor)
    +(parseFloat(perColorPrice)*inputNum_of_BColor);

    $('#total').html(formatCurrency(totalCost));
    $('#perShirt').html(formatCurrency(perShirtCost));
    $('#result').css('display', 'block');
    });
    });
    // This Function I have searched from Web
    function formatCurrency(strValue)
    {
    strValue = strValue.toString().replace(/\$|\,/g,'');
    dblValue = parseFloat(strValue);
    blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
    dblValue = Math.floor(dblValue*100+0.50000000001);
    intCents = dblValue%100;
    strCents = intCents.toString();
    dblValue = Math.floor(dblValue/100).toString();
    if(intCents<10)
    strCents = "0" + strCents;
    for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
    dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
    dblValue.substring(dblValue.length-(4*i+3));
    return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
    }
    -->
    </script>
    </head>
    <body>
    <form id="calc" name="calc">
    <p>
    <input id="shirts" name="shirts" type="text">
    <label for="shirts">Number of Shirts</label>
    </p>
    <p>
    <input id="colorsOnFront" name="colorsOnFront" type="text">
    <label for="colorsOnFront">Number of Colors on Front</label>
    </p>
    <p>
    <input id="colorsOnBack" name="colorsOnBack" type="text">
    <label for="colorsOnBack">Number of Colors on Back</label>
    </p>
    <p><input name="calculateTotal" id="calculateTotal" type="button" value="Calculate Total" /></p>
    <div id="result" style="display:none;"><strong>Total:</strong> <span id="total"></span> <strong>Per Shirt:</strong> <span id="perShirt"></span></div>
    </div>
    </form>
    </body>
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    Since you will never have a negative amount with your code, and since the values you are using are ALREADY floating numbers (thanks to your parseFloat calls) you can just use
    Code:
    function formatCurrency(num)
    {
        return "$" + num.toFixed(2);
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    And wouldn't it make more sense to do this:
    Code:
    var perShirtCost = (parseFloat(shirtsPrice))
        +(parseFloat(perColorPrice)*inputNum_of_FColor)
        +(parseFloat(perColorPrice)*inputNum_of_BColor);
    var totalCost = Number(inputNum_of_Shirts) * perShirtCose;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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