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
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Quick javascript help please

    I am new to java and tried writing the code for practice but it doesn't seem to work

    <html>
    <Head>
    <title>Discount</title>
    <script language ="Javascript">
    <!--
    function CalculateBalance(numItems,totPrice,discount)
    { var balance;
    balance = (1 - discount)*totPRice;
    alert ('Your balance is $'+balance +' Thank You.');
    document.write("<br>YOu bought "+ numItems+" items for $"+totPrice);
    document.write("<br>Your balance is $"+ balance+" your discount was"+discount*100+"% Thank you");
    }
    //-->
    </script>
    </head>

    <script language ="Javascript">
    <!--
    var discount;
    var numItems = parseInt(prompt('HOw many time did you buy [ >=12 is 20% and >= 6 is 10% discount]',"0"));
    alert ('You bought '+numItems +' items.');
    document.write("You bought "+ numItems+" items.");
    var totPrice = parseFloat(prompt('The total price that you paid was',"0.0"));
    if(numItems >= 12) {discount = .2;}
    else if (numItems >=6) {discount+ .1}
    else {discount = 0;}
    CalculateBalance(numItems,totPrice,discount);

    //-->
    </script>
    </html>

    It only prints to the screen the number of items I bought

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,082
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Kinda confused View Post
    I am new to java ..
    Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

    else if (numItems >=6) {discount+ .1}

    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. The first document.write wipes out your original page, including the script.

    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.


    Login: yes
    Password: i dont have one
    Password is incorrect

    Login: yes
    Password: incorrect
    Last edited by Philip M; 12-16-2010 at 05:10 PM.

  • #3
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Your code has a lot of errors not just in the javascript but in the mark-up coding as well, this code does what you want, but instead of just copy and paste it and then use it, I would take time to look over your code and then my code to see where the errors are and that way you will learn as you go.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Discount</title>
    <script type="text/javascript">
    (function($){
      /**
       * @Function: CalculateBalance :: PUBLIC
       * This calculates the discount price
       */
      $.CalculateBalance = function(numItems,totPrice,discount) {
        var balance;
        balance = (1 - discount)*totPrice;
        alert('Your balance is $'+balance+' Thank You.');
        return "You bought "+numItems+" items for $"+totPrice+
               "\nYour balance is $"+balance+" your discount was "+
               discount*100+"% Thank you";
      };
    
      /**
       * @Function: Discount :: PUBLIC
       * This returns the final discount price
       */
      $.Discount = function() {
        var discount,
            callBack,
            totPrice,
            numItems = parseInt(prompt('How many times did you buy\n[ >=12 is 20% and >= 6 is 10% discount]',"0"));
        if(numItems) {
          alert('You bought '+numItems+' items.');	
          totPrice = parseFloat(prompt('The total price that you paid was',"0.0"));
          if(numItems >= 12) {discount = .2;}	
          else if (numItems >= 6) {discount = .1;}
          else {discount = 0;}
          alert($.CalculateBalance(numItems,totPrice,discount));
        }
      };
    })(window);
    </script>
    </head>
    <body>
      <input type="button" value="Run Discount Checker" onclick="Discount();" />
    </body>
    </html>
    Last edited by DJCMBear; 12-16-2010 at 05:54 PM.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

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