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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question newbie! Script not working!

    Hey, im new at Javascript and hope someone can help me?

    I am trying to make a script to calculate shares and come up with a final value. This program will work out the gross and the net value and display it, to get the net there is a 3% commission of the gross. If the commission works out to be less that $30, it is $30. I am having trouble trying to work out why my code isnt even doing any calculations. Its meant to be calculating up to five different shares, but i was trying to get one working first.

    As i said i am new at this, hope someone can help me, here is my code:

    <HTML>
    <HEAD>
    <TITLE> Tracking Shares </TITLE>
    <script language = "javascript">
    function calculate() {
    var name = document.frmshares.name.value
    var number = document.frmshares.number.value
    var price = document.frmshares.price.value

    gross = number * price
    document.frmshares.gross.value = gross.toFixed(2)
    commission = 3/100 * gross

    if (commission < 30.00) {
    commission = 30.00
    }
    else
    {
    return false;
    }
    net = gross - commission
    window.document.frmshares.net.value = net.toFixed(2)
    }
    </script>
    </HEAD>

    <BODY>
    <h1>Tracking Shares</h1>
    <form name=frmshares action="action marker" onsubmit= "return calculate()"> <p>
    Share Name:<input type="text" name="name" size="28"><br>
    Number of Shares:<select size="1" name="number">
    <option value="10">10</option>
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="150">150</option>
    <option value="200">200</option>
    <option value="250">250</option>
    <option value="300">300</option>
    <option value="350">350</option>
    <option value="400">400</option>
    <option value="450">450</option>
    <option value="500">500</option>
    <option value="550">550</option>
    </select><br>
    Current Price:<input type="text" name="price"><br>
    <input type="submit" value="Calculate Tax" name = "btnSignUp" onsubmit = "calculate()"><br><br>
    Gross:<input type="text" name="gross"><br>
    Net:<input type="text" name="price"><br>
    </form>

    </BODY>
    </HTML>

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,529
    Thanks
    3
    Thanked 511 Times in 498 Posts
    Code:
    <HTML>
    <HEAD>
    <TITLE> Tracking Shares </TITLE>
    <script language = "javascript">
    function calculate() {
     var name = document.frmshares.name.value
     var number = document.frmshares.number.value||10
     var price = document.frmshares.price1.value
     if (isNaN(number)||isNaN(price)){
      alert('Numbers Only for Number and Price');
      return false;
     }
     var gross = number * price
     document.frmshares.gross.value = gross.toFixed(2)
     commission = 3/100 * gross
     if (commission < 30.00) {
      commission = 30.00
     }
     else {
     return false;
     }
     net = gross - commission
     document.frmshares.net.value = net.toFixed(2)
     return true;
    }
    </script>
    </HEAD>
    
    <BODY>
    <h1>Tracking Shares</h1>
    <form name=frmshares action="action marker" onsubmit= "return calculate()"> <p>
    Share Name:<input type="text" name="name" size="28"><br>
    Number of Shares:<select size="1" name="number">
    <option value="10">10</option>
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="150">150</option>
    <option value="200">200</option>
    <option value="250">250</option>
    <option value="300">300</option>
    <option value="350">350</option>
    <option value="400">400</option>
    <option value="450">450</option>
    <option value="500">500</option>
    <option value="550">550</option>
    </select><br>
    Current Price:<input type="text" name="price1"><br>
    <input type="submit" value="Calculate Tax" name = "btnSignUp" onsubmit = "return calculate()"><br><br>
    <input type="button" value="Calculate Tax" name = "" onclick = "return calculate()"><br><br>
    Gross:<input type="text" name="gross"><br>
    Net:<input type="text" name="net"><br>
    </form>
    </form>
    
    </BODY>
    </HTML>
    no duplicate variable and NAME name
    + check your names.

    best to use the code tags when posting, theres a button for them.

    and welcome
    Last edited by vwphillips; 06-03-2006 at 12:30 PM.
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question problem

    Ok this script works, until i add 4 more name, number and price fields. How do i make it work?

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thats because you can't have all the fields named the same. you can't have 4 text inputs that are all named "price" you need to name them like "price1", "price2", "price3", etc... so your specifying exactly which "price" field to pull the value from.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,529
    Thanks
    3
    Thanked 511 Times in 498 Posts
    Just had a pm to look at this post

    cant say much more than boxxer03 posts wihout more information

    also pms should not be to make me look, Its up to me if I wish to follow up a post unless you are a paying client
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #6
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    most people on here recieve e-mails when a new message is posted on a thread they've already posted on, basically telling them to check out the new message. anyways, if you want help to get the code working, you should probably post the new, full code your using now, on here.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #7
    New Coder
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    <HTML>
    <HEAD>
    <TITLE> Tracking Shares </TITLE>
    <script language = "javascript">
    function calculate() {
    var name = document.frmshares.name.value
    var number = document.frmshares.number.value
    var price = document.frmshares.price1.value
    if (document.frmshares.name.value = "") {
    alert("Please enter a share name.");
    document.frmshares.name.select();
    }
    if (document.frmshares.number.value = "") {
    alert("Please enter a number of shares.");
    document.frmshares.number.select();
    }
    if (document.frmshares.price1.value = "") {
    alert("Please enter a price.");
    document.frmshares.price1.select();
    }
    if (isNaN(number)||isNaN(price)){
    alert('Only numeric values in number of shares and the price field');
    return false;
    }
    var gross = number * price
    document.frmshares.gross.value = gross.toFixed(2)
    commission = 3/100 * gross
    if (commission < 30.00) {
    commission = 30.00
    }
    else {
    return false;
    }
    net = gross - commission
    document.frmshares.net.value = net.toFixed(2)
    return true;
    }
    </script>
    </HEAD>

    <BODY>
    <h1>Tracking Shares</h1>
    <form name="frmshares" action="action marker" onsubmit= "return calculate()"> <p>
    Share Name:<input type="text" name="name" size="28"><br>
    Number of Shares:<input type="text" name="number"><br>
    Current Price:<input type="text" name="price1"><br><br>


    <input type="button" value="Calculate Tax" name = "" onclick = "return calculate()"><br><br>
    Gross:<input type="text" name="gross"><br>
    Net:<input type="text" name="net"><br>
    </form>
    </form>

    </BODY>
    </HTML>

  • #8
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Ameslee
    Code:
    if (commission < 30.00) {
    		commission = 30.00
    	}
    	else {
    		return false;
    	 }
    The code seems to work after a fashion, but could you clarify why you're aborting when commission >=30? Also, what is supposed to happen when the commission exceeds the gross?

    Please note that when handling money, work in pennies/cents and then format the result, rather than performing calculations on floating point numbers.
    Last edited by Arty Effem; 06-05-2006 at 03:30 AM.

  • #9
    New Coder
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    The commission for seling the shareholding is calcualted as 3% of the gross value with a minimum of $30.00 being charged.

    If the commission works out to be less than $30 well it must be $30.

    Does this make sense?

  • #10
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Ameslee
    The commission for seling the shareholding is calcualted as 3% of the gross value with a minimum of $30.00 being charged.

    If the commission works out to be less than $30 well it must be $30.

    Does this make sense?
    Yes I knew that; unfortunately it has no bearing upon what I asked.

    (Take #2)
    In the segment that I quoted, if the commission is 30 or more, the function stops executing and does not display a result. Why?

    If the comission of 30 causes the net to be 0 or negative (I.E. a non-viable sale) what if any action should be taken?

  • #11
    New Coder
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    so your saying it should return true. I am a bit confused at the moment. Please take it easy with me i am only new at this and still learning.

    Is this the reason why it is not working when i add up 2 five lots of fields?

    thanks

  • #12
    New Coder
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Quote Originally Posted by boxxer03
    thats because you can't have all the fields named the same. you can't have 4 text inputs that are all named "price" you need to name them like "price1", "price2", "price3", etc... so your specifying exactly which "price" field to pull the value from.
    Doing it this way would make one huge long piece of code right, coz i would have to duplicate the code i already have 4 more times to suite the names i use. Right?

  • #13
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <html>
    </head>
    <title> Tracking Shares </title>
    <script type="text/javascript">
    function calculate(whichSet){
     var name=document.getElementById('name'+whichSet).value;
     var number=document.getElementById('number'+whichSet).value||10;
     var price=document.getElementById('price'+whichSet).value;
     if (isNaN(number)||isNaN(price)){
      alert('Numbers Only for Number and Price');
      return false;
     }
     var gross=number*price;
     document.getElementById('gross'+whichSet).value=gross.toFixed(2);
     commission=3/100*gross;
     if (commission<30){
      commission=30;
     }
     else {
      commission=commission;
     }
     net=gross-commission;
     document.getElementById('net'+whichSet).value=net.toFixed(2);
     return true;
    }
    </script>
    </head>
    
    <body>
    <h1>Tracking Shares</h1>
    <form id="frmshares" action="action marker">
    <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
    <tr>
    <td width="50%"><p>
    Share Name:<input type="text" id="name1" size="28"><br>
    Number of Shares:<select size="1" id="number1">
    <option value="10">10</option>
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="150">150</option>
    <option value="200">200</option>
    <option value="250">250</option>
    <option value="300">300</option>
    <option value="350">350</option>
    <option value="400">400</option>
    <option value="450">450</option>
    <option value="500">500</option>
    <option value="550">550</option>
    </select><br>
    Current Price:<input type="text" id="price1" size="20"><br>
    <input type="button" value="Calculate Tax" id="btnSignUp1" onClick="return calculate(1)"><br><br>
    Gross:<input type="text" id="gross1" size="20"><br>
    Net:<input type="text" id="net1" size="20"><br>
    </td>
    <td width="50%"><p>
    Share Name:<input type="text" id="name2" size="28"><br>
    Number of Shares:<select size="1" id="number2">
    <option value="10">10</option>
    <option value="50">50</option>
    <option value="100">100</option>
    <option value="150">150</option>
    <option value="200">200</option>
    <option value="250">250</option>
    <option value="300">300</option>
    <option value="350">350</option>
    <option value="400">400</option>
    <option value="450">450</option>
    <option value="500">500</option>
    <option value="550">550</option>
    </select><br>
    Current Price:<input type="text" id="price2" size="20"><br>
    <input type="button" value="Calculate Tax" id="btnSignUp2" onClick="return calculate(2)"><br><br>
    Gross:<input type="text" id="gross2" size="20"><br>
    Net:<input type="text" id="net2" size="20"><br>
    </td>
    </tr>
    </table>
    </form>
    </body>
    
    </html>
    this should work fine and it should work with any number of sets of input fields you want to add. i added the table just so you can visually see the seperation of the sets of inputs. anyways, what it does is at the top in red right after the 'function calculate' it says (whichSet) that gets the set number from the onClick function of the Calculate button, so if its the first set it returns 1, second set returns 2, etc... and all the form fields are id-ed to match (note: i changed them from name="" to id=""). so name="gross" is now id="gross1" or id="price1". so on and so forth for each set. just check it out, it should work fine, but ad far as if the calculations are correct, you'd have to check that because i don't know how stock calculations work.
    Last edited by boxxer03; 06-05-2006 at 08:03 AM.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #14
    New Coder
    Join Date
    Jun 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is it possible to have only one net and gross field where the totals for both are displayed? How would i do this?

  • #15
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, you can, but your getting into more coding.
    Last edited by boxxer03; 06-05-2006 at 09:20 AM.
    I'm only telling you how I would do it, not how its supposed to be done. ;)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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