shadowayex Posted January 23, 2008 Share Posted January 23, 2008 Ok, I want to add a grand total to my profit calculator. The Javascript codes I have are:Global Variables: function additem(){ numofitem=prompt("How many items would you like to add?\(Maximum of 50\)\nWarning: Only do this once!","Number Of Items"); numofitems=parseInt(numofitem)+1; if (numofitems<2 || numofitems>50) { alert("Please enter a number between 1 and 49."); } else { var i=2; for (i=2;i<=numofitems;i++) { document.getElementById('rows').innerHTML += "Item Name: "; document.getElementById('rows').innerHTML += "<input type=\"text\" id=\"item" + i + "\" value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;\ \;"; document.getElementById('rows').innerHTML += "Buying Price: "; document.getElementById('rows').innerHTML += "<input type=\"text\" id=\"buy" + i + "\" value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;\ \;"; document.getElementById('rows').innerHTML += "Selling Price: "; document.getElementById('rows').innerHTML += "<input type=\"text\" id=\"sell" + i + "\" value=\"\" size=\"15\" />\ \;\ \;\ \;\ \;\ \;"; document.getElementById('rows').innerHTML += "Amount Traded: "; document.getElementById('rows').innerHTML += "<input type=\"text\" id=\"amnt" + i + "\" value=\"\" size=\"15\" /><br />"; } }} Calculating Function: function calculate(){ var x=numofitems; var finalprofit=""; var n=1; for (n=1;n<=x;n++) { var item=document.getElementById("item" + n).value; var buy=document.getElementById("buy" + n).value; var sell=document.getElementById("sell" + n).value; var amnt=document.getElementById("amnt" + n).value; var totalbuy=buy*amnt; var totalsell=sell*amnt; var profit=totalsell-totalbuy; if (profit==0) { break; } else { finalprofit += "The profit for your " + item + "s is " + profit + ".\n"; } }alert(finalprofit);} I want to add a code to my calculating function that makes the last row the grand total. I tried a few ideas without any luck. Anyone else got any ideas? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 24, 2008 Share Posted January 24, 2008 Either have an element that you can reference with getElementById to write the total into, or create a new element in Javascript and append it to the table or something to write the total to. If you create a new tr you'll need to have a tbody element in the table to append it to, they don't like it when you try to append a tr directly to a table. Link to comment Share on other sites More sharing options...
shadowayex Posted January 28, 2008 Author Share Posted January 28, 2008 Well, I have the results displaying in an alert box. That kind of cuts out your answer :/ Link to comment Share on other sites More sharing options...
justsomeguy Posted January 28, 2008 Share Posted January 28, 2008 I want to add a code to my calculating function that makes the last row the grand total....Well, I have the results displaying in an alert box... you want the results to display in the last row of an alert box? I don't understand.Totals aren't something that should require explanation. If you want a total of several numbers, then add up those numbers. That should be pretty obvious. Once you have your total, you can alert it, or put it in a table, or put it in a box, or open a new window and display it there, or put it in a cookie, or whatever you want to do with it. I'm not sure what your question is. Link to comment Share on other sites More sharing options...
shadowayex Posted January 29, 2008 Author Share Posted January 29, 2008 Ok, I think there's a confusion of ideas. Maybe if you try out the profit calculator you'll understand better. Here's the link:http://profcalc.freehostia.com/Play around with the thing a little bit. What I want to do is when it displays everything, I want to add a line where all the profits are added in a total profit for all items. Understand? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 29, 2008 Share Posted January 29, 2008 I guess I don't understand, I'm not sure where the difficulty is. It seems obvious to me to add a line with the total in the alert box, and it seems obvious to add a row to the page with the total in it. Are you wanting to do one of those things, or something else? Link to comment Share on other sites More sharing options...
shadowayex Posted January 29, 2008 Author Share Posted January 29, 2008 I guess I don't understand, I'm not sure where the difficulty is. It seems obvious to me to add a line with the total in the alert box, and it seems obvious to add a row to the page with the total in it. Are you wanting to do one of those things, or something else?Well, all those numbers that pop up are stored into a single variable using +=. What I'm tring to figure out is how to get those numbers and add them together. Do you see what I'm saying? Take a look at the code and mess with it and I think you'll understand. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 29, 2008 Share Posted January 29, 2008 You're calculating the profit, if you use another variable to keep track of the total profit you can add that wherever you want. function calculate(){ var x=numofitems; var finalprofit=""; var item; var buy; var sell; var amnt; var totalbuy; var totalsell; var profit var total = 0; for (var n=1;n<=x;n++) { item=document.getElementById("item" + n).value; buy=document.getElementById("buy" + n).value; sell=document.getElementById("sell" + n).value; amnt=document.getElementById("amnt" + n).value; totalbuy=buy*amnt; totalsell=sell*amnt; profit=totalsell-totalbuy; total += profit; if (profit==0) break; else finalprofit += "The profit for your " + item + "s is " + profit + ".\n"; } finalprofit += "Total: " + total; alert(finalprofit);} Link to comment Share on other sites More sharing options...
shadowayex Posted January 29, 2008 Author Share Posted January 29, 2008 You know, I feel kind of stupid now. Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.