PDA

View Full Version : Issue getting function to calculate a total



Vidgie65
Oct 15th, 2010, 02:46 AM
I have created a function but it only half works. I can get the number of contributers to add up but not the total amount of dollars they contributed. I am not sure if my function is wrong or if I am just confused with what variable to output for the dollar amount total. Please assist.

My Function:

<script type="text/javascript">
function amountTotal() {
total = 0;
for (var i=0; i<=amount.length; i++) {
total+=amount[i];
}
return total;
}
</script>

My output files:

<div id=totals>
<script type="text/javascript">
document.write("<table border=1' cellspacing=1'>");
document.write("<tr><th id=sumTitle colspan=2'>Summary</th></tr>");
document.write("<tr><th>Contributors</th>");
document.write("<td>"+[i]+"</td>");
document.write("</tr>");
document.write("<tr><th>Amount</th>");
document.write("<td>$ +[total]+</td></tr>");
document.write("</table>");
</script>

Old Pedant
Oct 15th, 2010, 08:12 PM
The [ ] around those values is totally bogus.

And in the second one, you did the quoting wrong.



document.write("<td>" + i + "</td>");
...
document.write("<td>$" + total + "</td></tr>");

Vidgie65
Oct 16th, 2010, 12:33 PM
Thanks for the info ref the brackets. I made the change you suggested. The total amount still does not show. Maybe I have something else wrong???? I will review all the code again, but I was sure it had to be within the function or my write statement.

Philip M
Oct 16th, 2010, 03:48 PM
This ought to move you forward. Please study it and learn from it - compare with your original code. Study especially the difference between ordinary brackets or parentheses () and square brackets []. Note that we are not supposed to do your homework for you, but we may correct your work if you yourself have made a substantial effort. But the main problems are in the HTML, rather than the Javascript. Suggest you revise your understanding of HTML tables.


<script type="text/javascript">

var amount = [20,40,60,80,50]; // example contributions

function amountTotal() {
total = 0;
for (var i=0; i<amount.length; i++) { // note: < not <= - do you understand why?
total += amount[i];
}
return total.toFixed(2); // i.e dollars and cents
}

document.write("<table border=1 cellspacing=1>");
document.write("<tr><th colspan = 2>Summary</th></tr>");
document.write("<tr><th colspan = 2>Contributors And Amount</th></tr>");

for (var i = 0; i < amount.length; i++) {
document.write("<tr><td>" + (i+1) + "</td><td>" + amount[i].toFixed(2) + "</td></tr>"); // this is what you were groping for.
}

document.write("<tr><th>Total Amount</th>");
document.write("<td>$" + amountTotal() + "</td></tr>");
document.write("</table>");

</script>

Let us suppose that the names of the five contributors are Black, Green, White, Brown and Grey. Try changing your code to show the donor's name rather than just donor number 1, 2 etc.

A teacher informed my son that "There are two words which you should never use in school homework - one is cool and the other is gross". "No problem", replied the boy, "What are the two words?"

Vidgie65
Oct 20th, 2010, 03:46 AM
I understand your issue with homework. This was already a graded program. I needed to understand what I missed. The teacher does not provide this and it does me no good to keep going with a program that does not work fully. If I missed it here then I obviously will miss it again. I want to learn from my mistakes not just blow them off once I receive a grade.
Yes, I understand the note ref < versus <=
We are given the HTML and must provide the Javascript. I understand what you are saying ref the table, but our table had to say Contributors 35 and the next row had to say Amount with the total of money contributed by all 35.
Thank you for all your assistance.
(maybe this is not the forum for me as I am a beginning programmer)

Philip M
Oct 20th, 2010, 08:29 AM
Well, I have virtually done it for you. If you are unable to adapt the output to suit your exact requirements (35 contributors??) , then you really ought to ask yourself whether you are in the right class - not the right forum.:)

Vidgie65
Oct 21st, 2010, 02:50 AM
The assistance you provided helped. I realize I was calling the variable to print the total instead of calling the function that actually calculated the total.

You guys really dont need to be so rough to those of us who are trying to learn something new.

Thanks.

Philip M
Oct 21st, 2010, 08:54 AM
Don't expect to be treated like a fragile doll just because you're a newcomer. This forum is not intended to be a remedial teaching class. If experienced people spend some of their voluntary time helping you solve your problems you should make an effort yourself. You say I was rough (which I do not accept) but I did correct your code for you - thousands of other forum users simply ignored you. Think on.