Forming a javascript program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamesw23
    New Member
    • Apr 2010
    • 5

    Forming a javascript program

    I want to make a program using javascript that calculates the tax (at different price levels) on several different items. For instance, the user could enter in 5-6 different prices through a prompt, and the program would calculate the total (price + tax) and display the result. Once the user enters the number -1, that is when the program could stop and calculate the total.

    If the price is $100 or less, the tax would be 9%

    If the price is greater than $100, the tax would be 7%

    I'm pretty confused on how to go about doing this. Any help would be much appreciated.
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    You can easily do it by using a conditional statement,

    sample code:

    Code:
    var price = prompt ("Enter the price ","");
    var total=0;
    if(parseInt(price)>100)
    {
       total = parseInt(price) +  parseInt(price)* 7/100;
    }
    else
    {
       total = parseInt(price) +  parseInt(price)* 9/100;
    }
    This will do the work what you are looking for.

    Thanks and Regards
    Ramanan Kalirajan

    Comment

    • jamesw23
      New Member
      • Apr 2010
      • 5

      #3
      Awesome, thank you! Yeah, that works perfectly.

      What if I wanted to add multiple prices and then come out with a total at the end?

      For instance,

      5 items: $50, $70, $90, $110, $150

      and it would be two different tax brackets for each item.

      What would I add into the Javascript code in order to get this to work?

      Also, if I entered -1, it would make the program stop and give the total.

      Thanks in advance!

      Comment

      • RamananKalirajan
        Contributor
        • Mar 2008
        • 608

        #4
        Similar logic, its simple

        sample code,

        Code:
        var total=0;
        var flag=true;
        while(flag)
        {
        var price = prompt ("Enter the price ","");
        if(parseInt(price)!=-1)
        {
        if(parseInt(price)>100)
        {
           total+ = parseInt(price) +  parseInt(price)* 7/100;
        }
        else
        {
           total+ = parseInt(price) +  parseInt(price)* 9/100;
        }
        }
        else
        {
           alert("The total amount = "+total);
           flag=false;
        }
        }
        Thanks and Regards
        Ramanan Kalirajan

        Comment

        • jamesw23
          New Member
          • Apr 2010
          • 5

          #5
          Yeah, I've been playing around with the code and seeing all the different things it can do. Just getting started in Javascript and it's pretty interesting stuff, haha.

          One question, for the above code when I put it in a file with proper tags, etc. it doesn't work, the prompt doesn't display. Is there an error somewhere in the code? I've been researching JS and HTML errors and I couldn't find anything within it.

          Once again, I appreciate all the help :)

          Comment

          • RamananKalirajan
            Contributor
            • Mar 2008
            • 608

            #6
            There is nothing wrong in the code. I just added the braces and removed the white spaces. It works fine for me. Here goes the sample code.

            Code:
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
            <HTML>
            <HEAD>
            <TITLE> New Document </TITLE>
            <META NAME="Generator" CONTENT="EditPlus">
            <META NAME="Author" CONTENT="">
            <META NAME="Keywords" CONTENT="">
            <META NAME="Description" CONTENT="">
            </HEAD>
            
            <BODY>
            <script type="text/javascript">
            
            var total=0;
            var flag=true;
            while(flag)
            {
            	var price = prompt("Enter the price ","");
            	if(parseInt(price)!=-1)
            	{
            		if(parseInt(price)>100)
            		{
            		   total+= parseInt(price)+(parseInt(price)*(7/100));
            		}
            		else
            		{
            		   total+= parseInt(price)+(parseInt(price)*(9/100));
            		}
            	}
            	else
            	{
            	   alert("The total amount = "+total);
            	   flag=false;
            	}
            }
            
            </script>
            </BODY>
            </HTML>
            Thanks and Regards
            Ramanan Kalriajan

            Comment

            • jamesw23
              New Member
              • Apr 2010
              • 5

              #7
              Ahhhh okay. Yeah, that one works perfectly. Thank you.

              If you could, is there any particular website or place that you recommend to practice simple Javascript code like this?

              Comment

              • RamananKalirajan
                Contributor
                • Mar 2008
                • 608

                #8
                Best one is http://www.w3schools.com/js/default.asp for starters. You can learn a lot of stuffs from there. When u find free time just have a glance over this link http://crstylo.brinkster.net/articles/javascript.htm. I developed those articles present there.

                Thanks and Regards
                Ramanan Kalirajan

                Comment

                • jamesw23
                  New Member
                  • Apr 2010
                  • 5

                  #9
                  Awesome! Thank you again.

                  I'll definitely check out those websites and practice on my Javascript.

                  Comment

                  Working...