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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Help in converting javascript

    I am trying to convert following pesudocose to javascript. Dont know where am i messing it up. Any help will be great

    PSEUDOCODE:

    Program TaxCalc
    Read purchase

    If(purchase>=0 AND purchase<=7)
    tax=0
    else if(purchase>=8 AND purchase<=21)
    tax=1
    else if(purchase>=22 AND purchase<=35)
    tax=2
    else if(purchase>=36 AND purchase<=49)
    tax=3
    else if(purchase>=50 AND purchase<=64)
    tax=4
    else if(purchase>=65 AND purchase<=78)
    tax=5
    else if(purchase>=79 AND purchase<=92)
    tax=6
    else if(purchase>=93 AND purchase<=99)
    tax=7
    else print error the number should be >=0 and <=99

    print tax

    End Program

    JAVASCRIPT that I have so far


    <html>
    <body>
    <script type="text/javascript">
    // Program name: Tax Calculation
    // Purpose: Find tax for amounts under $1
    // Name: Niral Patel
    // Date: 2/15
    START
    //Declare variables
    var purchase; //Amount to be entered by user
    var tax; //Tax to be calculated

    //Welcome the user
    //Ask them to enter an amount less than a dollar

    document.write("This program will help you calculate tax" + BR);
    document.write("Please enter the amount, the amount has to be less than a dollar" + BR);

    purchase = prompt("Enter the amount for which tax is to be calculated:",ES);
    purchase = parseInt(purchase)


    if (purchase >= 0 ) {
    tax = 0
    }

    else if (purchase >= 7 ) {
    tax = 0
    }

    else if (purchase >= 21 ) {
    tax = 2
    }

    else if (purchase >= 35 ) {
    tax = 3
    }

    else if (purchase >= 49) {
    tax = 4
    }

    else if (purchase >= 64 ) {
    tax = 5
    }

    else if (purchase >= 78 ) {
    tax = 6
    }

    else if (purchase >= 92 ) {
    tax = 7
    }

    else if (purchase >= 93 ) {
    tax = 6
    }

    else if (purchase >= 99 ) {
    tax = 6
    }



    else {
    document.write("Number should be between 0 and 99." +BR);
    }
    display tax;

    Stop


    </script>
    </body>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Homework! Many errors, I am afraid.

    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 (including the Javascript which called it). 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 Javascipt for "and" is &&.
    if (purchase>=0 && purchase<=7) {tax = 0}

    There is no Javascript method "print" or "display"

    I don't understand the message ("Please enter the amount, the amount has to be less than a dollar"). Nor why if purchase >= 92 tax is 7 but if purchase >= 93 tax is 6.

    BR must be in quotes "<br>"

    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.

  • #3
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    > is greater than
    >= greater than or equal to

    what you should be doing is <= or <

    Code:
    function taxVal(x){
    var taxR = false;
    if( x<0 || x>99) return "Number Should be between 0 and 99";
    if( x<=7 ) taxR = 0;
    if( x<=21 ) taxR = 2;
    if( x<=35 ) taxR = 3;
    ... ans so on
    return taxR;
    }
    alert( taxVal(100) );
    then pass the function a number, if its in range the tax value is returned otherwise the message, all you do is test if the return value is numeric or not and then you can output the message or use the value returned

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by Cremator View Post
    > is greater than
    >= greater than or equal to

    what you should be doing is <= or <

    Code:
    function taxVal(x){
    var taxR = false;
    if( x<0 || x>99) return "Number Should be between 0 and 99";
    if( x<=7 ) taxR = 0;
    if( x<=21 ) taxR = 2;
    if( x<=35 ) taxR = 3;
    //... ans so on
    return taxR;
    }
    alert( taxVal(100) );
    then pass the function a number, if its in range the tax value is returned otherwise the message, all you do is test if the return value is numeric or not and then you can output the message or use the value returned
    No, that is not correct. The tax is always the highest possible value!


    Code:
    <script type = "text/javascript">
    
    function taxVal(x){
    x = Number(x) || 0;
    if( x<0 || x>99) return "Number Should be between 0 and 99";
    var taxR = 0;
    if( x>=8 ) taxR = 1;
    if( x>=22 ) taxR = 2;
    if( x>=36 ) taxR = 3;
    //... and so on
    return taxR;
    }
    
    alert( taxVal(6) );  // 0
    alert( taxVal(37) ); // 3
    </script>

    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.

  • #5
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    This does though
    Code:
    function taxVal(x){
    var taxR = false;
    if( x<0 || x>99) return "Number Should be between 0 and 99";
    if(!taxR && x<=7 ) taxR = 0;
    if(!taxR && x<=21 ) taxR = 2;
    if(!taxR && x<=35 ) taxR = 3;
    //... ans so on
    return taxR;
    }
    alert( taxVal(100) ); 
    alert( taxVal(6) );
    The main problems, getting ahead of myself and I left my glasses at work and I am not back at work for a few weeks, so your going to have to put up with typos'

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by Cremator View Post
    The main problems, getting ahead of myself and I left my glasses at work and I am not back at work for a few weeks, so your going to have to put up with typos'
    And poor grammar as well, I suppose.

    The code is still wrong!

    Ah! Typos! Not just completely incorrect and untested code, then.
    Last edited by Philip M; 02-17-2012 at 12:35 PM.

    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.

  • #7
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    Like I said, the OP can sort out whats wrong which is very simple to do as well.

  • #8
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks people! I finally was able to run it properly

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,893
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    Quote Originally Posted by Cremator View Post
    Like I said, the OP can sort out whats wrong which is very simple to do as well.
    Do you think your doctor, car repair garage, or plumber could stay in business with that attitude? Or indeed your employer? If you cannot be bothered to test your code before posting, then it would be better to keep quiet. I would be ashamed to offer stuff like that.

    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.


  •  

    Posting Permissions

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