www.webdeveloper.com
Results 1 to 3 of 3

Thread: help with javascript calculation

  1. #1
    Join Date
    Apr 2014
    Posts
    2

    help with javascript calculation

    Hello, I am having trouble with a script im working on. I am trying to get two inputs from a user via the prompt box as integers. Once the numbers reach over a hundred they should get an alert box. The problem is that it does the calculations wrong and I don't know what I'm doing wrong. Thank you!

    For a clearer explanation, this is my assignment:


    7.12 Develop a script that will determine whether a department-store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available:
    a) Account number
    b) Balance at the beginning of the month
    c) Total of all items charged by this customer this month
    d) Total of all credits applied to this customer's account this month
    e) Allowed credit limit
    The script should input each of these facts from a prompt dialog as an integer, calculate the new balance (= beginning balance + charges - credits), display the new balance and determine whether the new balance exceeds the customer's credit limit. For customers whose credit limit is exceeded, the script should output HTML5 text that displays the message "Credit limit exceeded."


    THIS IS MY HTML:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset = "utf-8">
    <title>
    </title>
    <script src="712.js"></script>
    </head>
    
    <body onLoad="checkBalance()">
    <p>Your purchase amounts</p>
    <ol id="charges">
    </ol>
    <br>
    <!-- <p>Your refund amounts</p>
    <ol id="refunds">
    </ol> -->
    </body>
    </html>

    THIS IS MY JAVASCRIPT
    Code:
    var accountActivity = [];//this is the charges and debits to the account
    
    
    function checkBalance()
    
    {
    
    	alert('hello, my name is omid nassir. welcome to my program. please click okay to continue');
    
    	var firstName = prompt('what is your first name?');
    	var lastName = prompt('what is your last name?');
    	var fullName = firstName + " " + lastName;
    
    
    	alert('Hello ' + fullName + '. Please enter the charges and refunds for your shopping this month, please click okay to continue');
    
    	var count = 0;
    	var total = 100;
    	while(count<total)
    	{
    		var charge = prompt('please enter charge');
    		var cost = parseInt(charge, 10);
    		accountActivity.push(cost);
    
    		var credit = -prompt('please enter refund');
    		var refund = parseInt(credit, 10);
    		accountActivity.push(refund);	
    		count = (count + cost) - refund;
    		document.write(count);
    		
    	
    if(count >total)
    
    	alert(fullName + ' you are spending too much!');
    document.getElementById('charges').innerHTML = "<li>" + accountActivity.join("</li><li>") + "</li>";
    }
    
    
    }
    Last edited by jedaisoul; 04-16-2014 at 03:37 PM. Reason: code tags added

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    Couple of suggestions.

    Don't use document.write after the page has been rendered. It causes the page to reload and start all over.

    Prompts are not the best way to enter data. OK as a beginner, but best to graduate to a <input type="text" ... soon.
    Also alerts should only be used for debugging the program, not for the final program.

    Your 'while' logic repeats until count < total. Might not never stop with that type of logic.

    I'm not sure I understand your logic for the count. What are you REALLY trying to do here.

    Suggest keeping a running balance to check against the addition of charges (credits) and deposits (refunds?)

    BTW: You should enclose your program between [ code] and [ /code] tags (without the spaces)
    to retain the code formatting and allow easier reading, copying, debugging, etc.

    Again, I'm not sure I understand the logic behind you original post, but here might be some changes to consider...
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset = "utf-8">
    <title>
    </title>
    <script src="712.js"></script>
    </head>
    
    <body onLoad="checkBalance()">
    <p>Your purchase amounts</p>
    <ol id="charges">
    </ol>
    <br>
    <!-- <p>Your refund amounts</p>
    <ol id="refunds">
    </ol> -->
    
    <div id="debug"></div>
    
    <script>
    // From: http://www.webdeveloper.com/forum/showthread.php?293375-help-with-javascript-calculation
    
    var accountActivity = [];//this is the charges and debits to the account
    
    function checkBalance() {
    /*  removed for testing purposes only
      alert('hello, my name is omid nassir. welcome to my program. please click okay to continue');
      var firstName = prompt('what is your first name?');
      var lastName = prompt('what is your last name?');
      var fullName = firstName + " " + lastName;
      alert('Hello ' + fullName + '. Please enter the charges and refunds for your shopping this month, please click okay to continue');
    */
    
      var count = 0;
      var total = 100;
      var done = false;
      while(count<total && !done) {
        var charge = prompt('please enter charge');  if (charge == '') { done = true; }
        var credit = prompt('please enter refund');  if (credit == '') { done = true; }
        if (! done) {
          var cost = parseInt(charge, 10);
          var refund = parseInt(credit, 10);
    
          accountActivity.push('Cost: '+cost+'   Refund: '+refund);
          count = (count + cost) - refund;  total += count;
          document.getElementById('debug').innerHTML += 'Activity: ' + count + '  Balance: '+ (total-cost+refund)+'<br>';
          if(count >total) alert(fullName + ' you are spending too much!');
          document.getElementById('charges').innerHTML = "<li>" + accountActivity.join("</li><li>") + "</li>";
        }
      }
    } 
    </script>
    </body>
    </html>

  3. #3
    Join Date
    Apr 2014
    Posts
    2
    Thank you very much for your input. I am very knew to javascript and this was an assignment for a class. But I know now to try to refrain from using those methods mentioned, however it was required for the assignment. Anyways I took a look at your code and it gave me better ways of thinking about the logic.

    Quote Originally Posted by JMRKER View Post
    Couple of suggestions.

    Don't use document.write after the page has been rendered. It causes the page to reload and start all over.

    Prompts are not the best way to enter data. OK as a beginner, but best to graduate to a <input type="text" ... soon.
    Also alerts should only be used for debugging the program, not for the final program.

    Your 'while' logic repeats until count < total. Might not never stop with that type of logic.

    I'm not sure I understand your logic for the count. What are you REALLY trying to do here.

    Suggest keeping a running balance to check against the addition of charges (credits) and deposits (refunds?)

    BTW: You should enclose your program between [ code] and [ /code] tags (without the spaces)
    to retain the code formatting and allow easier reading, copying, debugging, etc.

    Again, I'm not sure I understand the logic behind you original post, but here might be some changes to consider...
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset = "utf-8">
    <title>
    </title>
    <script src="712.js"></script>
    </head>
    
    <body onLoad="checkBalance()">
    <p>Your purchase amounts</p>
    <ol id="charges">
    </ol>
    <br>
    <!-- <p>Your refund amounts</p>
    <ol id="refunds">
    </ol> -->
    
    <div id="debug"></div>
    
    <script>
    // From: http://www.webdeveloper.com/forum/showthread.php?293375-help-with-javascript-calculation
    
    var accountActivity = [];//this is the charges and debits to the account
    
    function checkBalance() {
    /*  removed for testing purposes only
      alert('hello, my name is omid nassir. welcome to my program. please click okay to continue');
      var firstName = prompt('what is your first name?');
      var lastName = prompt('what is your last name?');
      var fullName = firstName + " " + lastName;
      alert('Hello ' + fullName + '. Please enter the charges and refunds for your shopping this month, please click okay to continue');
    */
    
      var count = 0;
      var total = 100;
      var done = false;
      while(count<total && !done) {
        var charge = prompt('please enter charge');  if (charge == '') { done = true; }
        var credit = prompt('please enter refund');  if (credit == '') { done = true; }
        if (! done) {
          var cost = parseInt(charge, 10);
          var refund = parseInt(credit, 10);
    
          accountActivity.push('Cost: '+cost+'   Refund: '+refund);
          count = (count + cost) - refund;  total += count;
          document.getElementById('debug').innerHTML += 'Activity: ' + count + '  Balance: '+ (total-cost+refund)+'<br>';
          if(count >total) alert(fullName + ' you are spending too much!');
          document.getElementById('charges').innerHTML = "<li>" + accountActivity.join("</li><li>") + "</li>";
        }
      }
    } 
    </script>
    </body>
    </html>

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