...

View Full Version : Stuck on a add and minus code with limitations



kurtulas
02-20-2007, 03:54 PM
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>

nikkiH
02-20-2007, 05:57 PM
Since you asked only for a hint... :D

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.

kurtulas
02-21-2007, 11:54 AM
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

nikkiH
02-21-2007, 03:11 PM
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:



var myVar = 12345;

meeeee
03-09-2007, 07:59 AM
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

felgall
03-09-2007, 08:06 AM
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.

meeeee
03-09-2007, 08:19 AM
<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum