Calculating and Display a Running Total

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Calculating and Display a Running Total

Post by oscardog »

So I have the following code:

Code: Select all

function calcbatsmen (num, letter, divID) {
var sum = 0;  
var tempVal = 0;
var tempNum = 0;
var name = '';
//Retrieve Data && Calculate batsmen score
for (i = 1; i <=7; i++) {
      name = 'ball' + num + '.' + i + letter; // construct the element name
      tempVal = document.getElementsByName(name)[0].value.toLowerCase();
      switch (tempVal) {
            case "b" : case "hw" : case "i" : case "c" : case "l" : case "r" : case "mk" : case "s" : case "pm":
                tempVal = -5;
                break;
            case "w": case "n":
                tempVal = 2;
                break;
            case "pp":
                tempVal = 5;
                break;
            default:
                tempVal = tempVal;
      }
      tempNum = Number(tempVal);
      sum = sum + tempNum;
    }
document.getElementById(divID).innerHTML = sum;
 
//Calculate Total Over Score
var name1 = 'o' + num + 'b1total';
var name2 = 'o' + num + 'b2total';
var getVar1 = document.getElementById(name1).innerHTML;
var getVar2 = document.getElementById(name2).innerHTML;
var num1 = Number(getVar1);
var num2 = Number(getVar2);
var overallTotal = num1 + num2;
var displayTotalName = 'o' + num + 'battotal';
document.getElementById(displayTotalName).innerHTML = overallTotal;
 
//Calculate running total
for (i = 0; i < 16; i++) {
    var tempRunNum = 0;
    var prevScore = 0;
    var runningDisplayTotal = 0;
    var nextRunningTotalNum = i + 1;
    var nextRunningTotalName = 'o' + nextRunningTotalNum + 'runningtotal';
    if(document.getElementById(nextRunningTotalName).innerHTML == 0) {
    var nextRunningTotal = document.getElementById(nextRunningTotalName).innerHTML;
    var nullCheck = true;
    }
 
if(i == 0 && nextRunningTotal == 0 && nullCheck == true) {
    var runningTotalName = 'o' + i + 'runningtotal';
    document.getElementById(runningTotalName).innerHTML = overallTotal;
}
if(i > 0 && nextRunningTotal > 0 && nullCheck == true) {
    tempRunNum = i - 1;
    var runningName = 'o' + tempRunNum + 'runningtotal';
    var prevScore = document.getElementById(runningName).innerHTML;
    prevScore = Number(prevScore);
    runningDisplayTotal = overallTotal + prevScore;
    alert(runningDisplayTotal);
    var runningTotalName = 'o' + i + 'runningtotal';
    document.getElementById(runningTotalName).innerHTML = runningDisplayTotal;
    
}
}
}
As you can see from the comments, the running total is the bottom part and the top part does something else and pretty irrelevant. Anyway what I want to do is get the previous score from a DIV, which will one less than i(unless i == 0) and then add that score to another score and display it. But, the part that wont quite work is I only want it to this if the div(which will be i +1) is not == 0 which is the problem part.

At the moment I get the error(from firebug) as:
document.getElementById(nextRunningTotalName) is null
calcbatsmen(Object name=num value=0, "a", "o0b1total")myJS.js (line 46)
function onchange(event) { calcbatsmen(0, "a", "o0b1total"); }(change )scoreshe...ent/seq/1 (line 2)
[Break on this error] if(document.getElementById(nextRunningTotalName).innerHTML == 0) {\n
And the var nextRunningTotalName is set right, it just wont seem to work :S

Any ideas?
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Calculating and Display a Running Total

Post by kaszu »

Error says there is no element with that ID. Check your html.

Also following will never be true, because i is 15 after for loop:

Code: Select all

if(i == 0 && nextRunningTotal == 0 && nullCheck == true) {
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Calculating and Display a Running Total

Post by oscardog »

kaszu wrote:Error says there is no element with that ID. Check your html.

Also following will never be true, because i is 15 after for loop:

Code: Select all

if(i == 0 && nextRunningTotal == 0 && nullCheck == true) {
I have fixed the loop, al vars in the loop now use e instead of i to avoid any confusion :)

The HTML is right, there is a DIV ID which matches the one in the JS. For some reason it just wont work :S

Oscardog
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Calculating and Display a Running Total

Post by kaszu »

If possible, please post html.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Calculating and Display a Running Total

Post by oscardog »

kaszu wrote:If possible, please post html.
Well rather than spam this topic with a LONG post I pasted it into pastebin :)

http://pastebin.com/m69289caf

That is the rendered HTML, obviously, that is seen when using View Source :)

Oscardog
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Calculating and Display a Running Total

Post by kaszu »

Code: Select all

var nextRunningTotalNum = i + 1;
var nextRunningTotalName = 'o' + nextRunningTotalNum + 'runningtotal';
should be

Code: Select all

var nextRunningTotalNum = i;
var nextRunningTotalName = 'o' + nextRunningTotalNum + 'runningtotal';
because you have ids from o0runningtotal to o15runningtotal
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Calculating and Display a Running Total

Post by oscardog »

Fixed that :) No more errors, now I just have to get it to work right.

What it should do is get the value from the overbefore(so for example o1runningtotal) and then add that with the total from the current over(Stored in o2battotal) add them together adn THEN display in o2running total.

But ofc the numbers(so after the o) are dynamic and change depending on the loop value.

At the moment, it doesn't quite work and seems to display(for example) displays o1runningtotal in o0runningtotal.

Here is a screenie:

Image

Hope I made sense :S
Post Reply