www.webdeveloper.com
Results 1 to 6 of 6

Thread: MakeChange.html (have difficulty writing this code)

  1. #1
    Join Date
    Feb 2011
    Posts
    3

    MakeChange.html (have difficulty writing this code)

    Here's the task that I am having a little difficulty with:

    This Web page is to calculate the change that should be given to a customer as they pay for a purchase. Ask the
    user for the amount of the purchase and the amount of cash provided by the customer. When the user clicks on a
    button you provide, it should calculate and display how many of each of the following needs to be given to the
    customer to provide the correct change.
     $100 bill
     $20 bill
     $10 bill
     $5 bill
     $1 bill
     Quarter
     Dime
     Nickel
     Penny

    I'm pretty new to writing html, and not quite sure how I would do this for my assignment. If anyone could help that would be tremendous.

  2. #2
    Join Date
    Mar 2009
    Posts
    523
    Try to write the code yourself, and post it here when and if it doesn't work. If you want someone to do your homework for you then hire somebody to do it for you--and they will probably want the cash up front. If you won't make the effort to try to write the code for yourself why should anyone else bother to make such an effort for you?

  3. #3
    Join Date
    Feb 2011
    Posts
    3

    Reply

    Thanks for the feedback, here's what I have so far:


    <html>

    <head>

    <title> Display Correct Change </title>

    <!------------------------------>

    <script type="text/javascript">

    function DisplayOperators()

    // Assumes: value1 and value2 have values

    // Results: writes values to text boxes

    {



    changeleft = totaldllrgvn totalamtprchs;
    document.getElementById('t_subtract').value = totaldllrgvn + ' minus ' + totalamtprchs + ' equals ' + changeleft;
    document.getElementById('t_hundreds').value = Math.floor(changeleft / 100);
    changeleft = changeleft % 100;
    document.getElementById('t_fifties').value = Math.floor(changeleft / 50);
    changeleft = changeleft % 50;
    document.getElementById('t_twenties').value = Math.floor(changeleft / 20);
    changeleft = changeleft % 20;
    document.getElementById('t_tens').value = Math.floor(changeleft / 10);
    changeleft = changeleft % 10;
    document.getElementById('t_fives').value = Math.floor(changeleft / 5);
    changeleft = changeleft % 5;
    document.getElementById('t_ones').value = Math.floor(changeleft / 1);
    changeleft = changeleft % 1;



    }

    </script>



    </head>

    <body>

    <h2 style="text-align: center">

    Display Correct Change

    </h2>

    <hr />

    <!---------------------------------------------------------------------

    <!-- Define the user input area -->

    <p>

    <textarea id="t_introBox" rows="4" cols="40">

    To see total amount of change back
    Click the Collect Change! button.

    </textarea>

    <br />

    Total Amount of the Purchase:

    <input type="text" id="t_totalamtprchs" size="10" value="" />

    <br />

    Total Dollar Amount Given:

    <input type="text" id="t_totaldllrgvn" size="10" value="" />

    <br />

    </p>

    <!-------------------------------------------------------

    <!-- Define the button and react to the click -->

    <!------------------------------>

    <p>

    <input type="button" value="Collect Change!" style="background-color: Green; color: White"

    onclick="totalamtprchs = parseFloat(document.getElementById('t_totalamtprchs').value);

    totaldllrgvn = parseFloat(document.getElementById('t_totaldllrgvn').value);

    DisplayOperators();

    DisplayFunctions();

    " />

    </p>

    <hr />

    <!------------------------------>



    <!------------------------------>

    <h3>

    Total Change Back

    </h3>

    <p>



    <b>Total Amount Back: </b>

    <input type="text" id="t_subtract" size="60" value="" />

    <br />
    <b>Total Number in Hundreds Given Back: </b>
    <input type="text" id="t_hundreds" size="60" value="" />
    <br />
    <b>Total Number in Fifties Given Back: </b>
    <input type="text" id="t_fifties" size="60" value="" />
    <br />
    <b>Total Number in Twenties Given Back: </b>
    <input type="text" id-"t_twenties" size="60" value="" />
    <br />
    <b>Total Amount in Tens Given Back: </b>
    <input type="text" id="t_tens" size="60" value="" />
    <br />
    <b>Total Amount in Fives Given Back: </b>
    <input type="text" id="t_fives" size="60" value="" />
    <br />
    <b>Total Amount in Ones Given Back: </b>
    <input type="text" id="t_ones" size="60" value="" />
    <br />


    </p>
    <hr />
    </body>
    </html>

  4. #4
    Join Date
    Mar 2009
    Posts
    523
    This is weird and bizarre. What kind of keyboard are you using? In your line of code in your DisplayOperators() function
    Code:
    changeleft = totaldllrgvn – totalamtprchs;
    the 'minus' symbol is different from what comes from my keyboard. When I eliminated it and replaced it with what passes for a 'minus' on my keyboard your first field did indeed produce the desired effect for the field with the id 't_subtract' whereas it didn't before.

  5. #5
    Join Date
    Mar 2009
    Posts
    523
    I looked at your code again. Actually it does work if, besides what I pointed out above:

    1. Change your HTML where it defines the id for 't_twenties' It should be id = 't_twenties' You have id - 't_twenties'. A simple typo.

    2. Your script is calling a function called 'DisplayFunctions();' but its not defined anywhere. I commented it out, and made the changes indicated above, and it does work.

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Any document needs a Doctype. The way you have coded, your document needs an XHTML Doctype. If so, your comments like <!------------------------------> should disappear. The standard HTML comment should look precisely as
    Code:
    <!-- comment text inside -->
    Under XHTML the embedded javascript code should be encapsulated in CDATA islands
    Code:
    <script type="text/javascript">
    /*<![CDATA[*/
    
    /*]]>*/
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles