The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Using the function calculate script to compute the total for var, Adding a new variable to get a total for cells
Palmer55555
post Jan 17 2021, 07:25 PM
Post #1





Group: Members
Posts: 1
Joined: 17-January 21
Member No.: 27,732



This script gives an average price for the total number of shares bought. What I would like to add is the actual total of all the shares. I'm not much of a coder and I have tried to do this for the past few days and gave up. Can't get it correct. What I would like to add is to have the Total Number of Shares shown under the # of Shares entry column and the Average Price under the Purchase Price entry column. Any help would be greatly appreciated.

Here is the script:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<h2>Average Down Calculator</h2>
<p>Whether you are average down or average up, you can use the stock average down calculator to calculate the average price you paid for your shares.</p>
<table>
<tr>
<th width="10"></th>
<th width="180"># of Shares</th>
<th width="180">Purchase Price</th>
</tr>
<tr>
<td>1. </td>
<td><input id="shares1" type="text" onkeyup="calculate()"></td>
<td><input id="price1" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>2. </td>
<td><input id="shares2" type="text" onkeyup="calculate()"></td>
<td><input id="price2" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>3. </td>
<td><input id="shares3" type="text" onkeyup="calculate()"></td>
<td><input id="price3" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>4. </td>
<td><input id="shares4" type="text" onkeyup="calculate()"></td>
<td><input id="price4" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>5. </td>
<td><input id="shares5" type="text" onkeyup="calculate()"></td>
<td><input id="price5" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>6. </td>
<td><input id="shares6" type="text" onkeyup="calculate()"></td>
<td><input id="price6" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>7. </td>
<td><input id="shares7" type="text" onkeyup="calculate()"></td>
<td><input id="price7" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>8. </td>
<td><input id="shares8" type="text" onkeyup="calculate()"></td>
<td><input id="price8" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>9. </td>
<td><input id="shares9" type="text" onkeyup="calculate()"></td>
<td><input id="price9" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<td>10. </td>
<td><input id="shares10" type="text" onkeyup="calculate()"></td>
<td><input id="price10" type="text" onkeyup="calculate()"></td>
</tr>
<tr>
<th></th>
<th colspan="2" align="left"><br />Average Price: <span id="result" style="font-size:30px;color:#328ad1;"><span></td>
</tr>
</table>
</td></tr></table>
</td>
</tr>
</table>
<script>
function calculate()
{
var totalShares = 0;
var totalPrice = 0;
var average = 0;
var shares1 = parseFloat(document.getElementById('shares1').value);
var price1 = parseFloat(document.getElementById('price1').value);
var shares2 = parseFloat(document.getElementById('shares2').value);
var price2 = parseFloat(document.getElementById('price2').value);
var shares3 = parseFloat(document.getElementById('shares3').value);
var price3 = parseFloat(document.getElementById('price3').value);
var shares4 = parseFloat(document.getElementById('shares4').value);
var price4 = parseFloat(document.getElementById('price4').value);
var shares5 = parseFloat(document.getElementById('shares5').value);
var price5 = parseFloat(document.getElementById('price5').value);
var shares6 = parseFloat(document.getElementById('shares6').value);
var price6 = parseFloat(document.getElementById('price6').value);
var shares7 = parseFloat(document.getElementById('shares7').value);
var price7 = parseFloat(document.getElementById('price7').value);
var shares8 = parseFloat(document.getElementById('shares8').value);
var price8 = parseFloat(document.getElementById('price8').value);
var shares9 = parseFloat(document.getElementById('shares9').value);
var price9 = parseFloat(document.getElementById('price9').value);
var shares10 = parseFloat(document.getElementById('shares10').value);
var price10 = parseFloat(document.getElementById('price10').value);
if(!isNaN(shares1)&&!isNaN(price1))
{
totalShares += shares1;
totalPrice = shares1*price1;
}
if(!isNaN(shares2)&&!isNaN(price2))
{
totalShares += shares2;
totalPrice = totalPrice + shares2*price2;
}
if(!isNaN(shares3)&&!isNaN(price3))
{
totalShares += shares3;
totalPrice = totalPrice + shares3*price3;
}
if(!isNaN(shares4)&&!isNaN(price4))
{
totalShares += shares4;
totalPrice = totalPrice + shares4*price4;
}
if(!isNaN(shares5)&&!isNaN(price5))
{
totalShares += shares5;
totalPrice = totalPrice + shares5*price5;
}
if(!isNaN(shares6)&&!isNaN(price6))
{
totalShares += shares6;
totalPrice = totalPrice + shares6*price6;
}
if(!isNaN(shares7)&&!isNaN(price7))
{
totalShares += shares7;
totalPrice = totalPrice + shares7*price7;
}
if(!isNaN(shares8)&&!isNaN(price8))
{
totalShares += shares8;
totalPrice = totalPrice + shares8*price8;
}
if(!isNaN(shares9)&&!isNaN(price9))
{
totalShares += shares9;
totalPrice = totalPrice + shares9*price9;
}
if(!isNaN(shares10)&&!isNaN(price10))
{
totalShares += shares10;
totalPrice = totalPrice + shares10*price10;
}
if(totalShares != 0)
{
average = totalPrice / totalShares;
document.getElementById("result").innerHTML= "$"+average.toFixed(4);
} else {
document.getElementById("result").innerHTML= "";
}
}
</script>
</body>
</html>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 18th March 2024 - 11:27 PM