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

    Stuck on a add and minus code with limitations

    Hi All,

    Iv been writing Javascript codes for about 10 weeks now and was getting better until I came across this one.
    The code is all about shareholdings, currently ‘Silent Springs’ has 104 shares. I can buy or sell shares to and from 104 but the rules are that the company cannot hold more that 100000 or sell more shares than they hold. I can also get the ‘Warning’ sign to appear if the rules have been broken but my problem is the code wont reset and see’s ‘Warning’ as the end when I want it to reset to the last value entered so I can buy or sell again.
    As this is part of an assignment for a degree I am doing I would rather not have someone post the answer for me but explain what I have done wrong and explain how to rectify it.

    Thank you, Kurt.
    <html>
    <head> MAMMON & GREED - SHARE TRADING SYSTEM

    <title>M150 TMAO3 Question 3 (ii) </title>

    <script language="JavaScript" >

    var companies = ['SS','MA','RB','PC'];
    var sharePrices = [25.44,83.33,88.89,24.44];
    var shareHoldings = [104,23745,12945,84000];

    function refreshHoldings()
    {
    document.holdings.ssHold.value = shareHoldings[0]
    document.holdings.mdHold.value = shareHoldings[1]
    document.holdings.rbHold.value = shareHoldings[2]
    document.holdings.pcHold.value = shareHoldings[3]
    }
    function buyShares(Type,Amount)
    {
    shareHoldings[Type] = shareHoldings[Type] + parseFloat(Amount)
    }
    function sellShares(Type,Amount)
    {
    shareHoldings[Type] = shareHoldings[Type] - parseFloat(Amount)
    }


    function refreshHoldings()
    {
    if (shareHoldings[0] > 100000 || shareHoldings[0] < 0)
    {
    shareHoldings[0] = ('Warning')
    }
    else
    if (shareHoldings[1] > 100000 || shareHoldings[1] < 0)
    {
    shareHoldings[1] = ('Warning')
    }
    else
    if (shareHoldings[2] > 100000 || shareHoldings[2] < 0)
    {
    shareHoldings[2] = ('Warning')
    }
    else
    if (shareHoldings[3] > 100000 || shareHoldings[3] < 0)
    {
    shareHoldings[3] = ('Warning')
    }
    else
    {
    document.holdings.ssHold.value = shareHoldings[0]
    document.holdings.mdHold.value = shareHoldings[1]
    document.holdings.rbHold.value = shareHoldings[2]
    document.holdings.pcHold.value = shareHoldings[3]

    }
    }
    </script>

    </head>

    <body>
    <FORM NAME = "trading">
    <BR>
    You may buy or sell the following shares
    <BR><BR>
    Silent Spring Petrochemicals plc
    <BR>
    <INPUT TYPE = "button"
    VALUE = "Buy"
    ONCLICK = "buyShares(0,document.trading.ssBuy.value)" >
    <INPUT TYPE = "text"
    NAME = "ssBuy"
    VALUE = "" >
    <INPUT TYPE = "button"
    VALUE = "Sell"
    ONCLICK = "sellShares(0,document.trading.ssSell.value)" >
    <INPUT TYPE = "text"
    NAME = "ssSell"
    VALUE = "">
    <BR>
    Megadeath Armaments Corp.
    <BR>
    <INPUT TYPE = "button"
    VALUE = "Buy"
    ONCLICK = "buyShares(1,document.trading.mdBuy.value)" >
    <INPUT TYPE = "text"
    NAME = "mdBuy"
    VALUE = "" >
    <INPUT TYPE = "button"
    VALUE = "Sell"
    ONCLICK = "sellShares(1,document.trading.mdSell.value)" >
    <INPUT TYPE = "text"
    NAME = "mdSell"
    VALUE = "">
    <BR>
    Reckitt and Bodger plc
    <BR>
    <INPUT TYPE = "button"
    VALUE = "Buy"
    ONCLICK = "buyShares(2,document.trading.rbBuy.value)" >
    <INPUT TYPE = "text"
    NAME = "rbBuy"
    VALUE = '' >
    <INPUT TYPE = "button"
    VALUE = "Sell"
    ONCLICK = "sellShares(2,document.trading.rbSell.value)" >
    <INPUT TYPE = "text"
    NAME = "rbSell"
    VALUE = "">
    <BR>
    Pravda Communications Corp
    <BR>
    <INPUT TYPE = "button"
    VALUE = "Buy"
    ONCLICK = "buyShares(3,document.trading.pcBuy.value)" >
    <INPUT TYPE = "text"
    NAME = "pcBuy"
    VALUE = ''>
    <INPUT TYPE = "button"
    VALUE = "Sell"
    ONCLICK = "sellShares(3,document.trading.pcSell.value)" >
    <INPUT TYPE = "text"
    NAME = "pcSell"
    VALUE = "">
    <BR>
    </FORM>

    <FORM NAME = "holdings">
    <BR>
    You hold the following shares
    <BR><BR>
    Silent Spring Petrochemicals plc.
    <BR>
    <INPUT NAME = "ssHold"
    "TYPE = "text"
    VALUE = '' >
    <BR>
    Megadeath Armaments Corp.
    <BR>
    <INPUT NAME = "mdHold"
    "TYPE = "text"
    VALUE = '' >
    <BR>
    Reckitt and Bodger plc.
    <BR>
    <INPUT NAME = "rbHold"
    "TYPE = "text"
    VALUE = '' >
    <BR>
    Pravda Communications Corp.
    <BR>
    <INPUT NAME = "pcHold"
    "TYPE = "text"
    VALUE = '' >
    <BR>
    Press button to update.............
    <INPUT TYPE = "button"
    VALUE = "Refresh holdings"
    ONCLICK = "refreshHoldings()">
    </FORM>
    </body>

    </body>

    </html>

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Since you asked only for a hint...

    I see code to set the warning in IF statements.
    I do not see ELSE statements to remove it, had it already been set.

    This is not re-allocated when the button is pressed:
    var shareHoldings = [104,23745,12945,84000];

    So if you had set
    shareHoldings[0] = ('Warning')
    it stayed that way.

    Also, please use the CODE tags to format your code when you post it so it is more readable.
    Last edited by nikkiH; 02-20-2007 at 06:03 PM.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks NikkiH,
    Your a genius iv cracked!!! declare Warning as a variable and set it to '0' then change shareHoldings[0] ('Warning') to buyShares amount. Its fixed my code but created another problem Warning comes up but hidden the numbers keep on adding or taking away, ill try to sort this myself but thanks again.

    One more thing sorry about the way i inserted my code but what do you mean by use code tags???

    Kurt

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    No problem.
    A suggestion: associative arrays are better than many arrays where the indexes supposedly match up.

    Code tag is the little # sign when you post, or you can manually type in
    [ code ]
    [ /code ]
    (no spaces)

    It formats the code all pretty like:

    Code:
    var myVar = 12345;

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi i am also having the same problems with my code. I can get the the warning message to be displayed however when another value is entered it stays the same.I have no idea how to fix this problem.Any ideas would be great

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,611
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by meeeee View Post
    Hi i am also having the same problems with my code. I can get the the warning message to be displayed however when another value is entered it stays the same.I have no idea how to fix this problem.Any ideas would be great
    Well without seeing the code it is impossible to say what the problem might be.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <html>
    <head> MAMMON & GREED - SHARE TRADING SYSTEM
    
    <title>M150 TMAO3 Question 3 (ii) </title>
    
    <script language="JavaScript" > 
    
    var companies = ['SS','MA','RB','PC'];
    var sharePrices = [25.44,83.33,88.89,24.44];
    var shareHoldings = [104,23745,12945,84000];
    
    
    
    
    function refreshHoldings()
    {
    document.holdings.ssHold.value = shareHoldings[0]
    document.holdings.mdHold.value = shareHoldings[1]
    document.holdings.rbHold.value = shareHoldings[2]
    document.holdings.pcHold.value = shareHoldings[3]
    }
    function buyShares(Type,Amount)
    {
    shareHoldings[Type] = shareHoldings[Type] + parseFloat(Amount)
    }
    function sellShares(Type,Amount)
    {
    shareHoldings[Type] = shareHoldings[Type] - parseFloat(Amount)
    }
    
    
    function refreshHoldings()
    {
    if (shareHoldings[0] > 100000 || shareHoldings[0] < 104)
    {
    shareHoldings[0] = ('Warning')
    }
    else 
    { 
    reset (shareHoldings[0]);
    } 
    
    
    else
    if (shareHoldings[1] > 100000 || shareHoldings[1] < 0)
    {
    shareHoldings[1] = ('Warning')
    }
    else
    if (shareHoldings[2] > 100000 || shareHoldings[2] < 0)
    {
    shareHoldings[2] = ('Warning')
    }
    else
    if (shareHoldings[3] > 100000 || shareHoldings[3] < 0)
    {
    shareHoldings[3] = ('Warning')
    }
    else
    {
    document.holdings.ssHold.value = shareHoldings[0]
    document.holdings.mdHold.value = shareHoldings[1]
    document.holdings.rbHold.value = shareHoldings[2]
    document.holdings.pcHold.value = shareHoldings[3]
    
    }
    }
    </script>
    
    </head>
    
    <body>
    <FORM NAME = "trading">
    <BR>
    You may buy or sell the following shares
    <BR><BR>
    Silent Spring Petrochemicals plc
    <BR>
    <INPUT TYPE = "button"
    VALUE = "Buy"
    ONCLICK = "buyShares(0,document.trading.ssBuy.value)" >
    <INPUT TYPE = "text"
    NAME = "ssBuy"
    VALUE = "" >
    <INPUT TYPE = "button"
    VALUE = "Sell"
    ONCLICK = "sellShares(0,document.trading.ssSell.value)" >
    <INPUT TYPE = "text"
    NAME = "ssSell"
    VALUE = "">
    <BR>
    Megadeath Armaments Corp.
    <BR>
    <INPUT TYPE = "button"
    VALUE = "Buy"
    ONCLICK = "buyShares(1,document.trading.mdBuy.value)" >
    <INPUT TYPE = "text"
    NAME = "mdBuy"
    VALUE = "" >
    <INPUT TYPE = "button"
    VALUE = "Sell"
    ONCLICK = "sellShares(1,document.trading.mdSell.value)" >
    <INPUT TYPE = "text"
    NAME = "mdSell"
    VALUE = "">
    <BR>
    Reckitt and Bodger plc
    <BR>
    <INPUT TYPE = "button"
    VALUE = "Buy"
    ONCLICK = "buyShares(2,document.trading.rbBuy.value)" >
    <INPUT TYPE = "text"
    NAME = "rbBuy"
    VALUE = '' >
    <INPUT TYPE = "button"
    VALUE = "Sell"
    ONCLICK = "sellShares(2,document.trading.rbSell.value)" >
    <INPUT TYPE = "text"
    NAME = "rbSell"
    VALUE = "">
    <BR>
    Pravda Communications Corp
    <BR>
    <INPUT TYPE = "button"
    VALUE = "Buy"
    ONCLICK = "buyShares(3,document.trading.pcBuy.value)" >
    <INPUT TYPE = "text"
    NAME = "pcBuy"
    VALUE = ''>
    <INPUT TYPE = "button"
    VALUE = "Sell"
    ONCLICK = "sellShares(3,document.trading.pcSell.value)" > 
    <INPUT TYPE = "text"
    NAME = "pcSell"
    VALUE = "">
    <BR>
    </FORM>
    
    <FORM NAME = "holdings">
    <BR>
    You hold the following shares
    <BR><BR>
    Silent Spring Petrochemicals plc.
    <BR>
    <INPUT NAME = "ssHold"
    "TYPE = "text"
    VALUE = '' >
    <BR>
    Megadeath Armaments Corp.
    <BR>
    <INPUT NAME = "mdHold"
    "TYPE = "text"
    VALUE = '' >
    <BR>
    Reckitt and Bodger plc.
    <BR>
    <INPUT NAME = "rbHold"
    "TYPE = "text"
    VALUE = '' >
    <BR>
    Pravda Communications Corp.
    <BR>
    <INPUT NAME = "pcHold"
    "TYPE = "text"
    VALUE = '' >
    <BR>
    Press button to update.............
    <INPUT TYPE = "button"
    VALUE = "Refresh holdings"
    ONCLICK = "refreshHoldings()">
    </FORM>
    </body>
    
    </body>
    
    </html>


  •  

    Posting Permissions

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