0
function checkData() {
    var temp = 0;
    var totalMarks = countMark(temp);
    if (totalMarks != 100)
        window.alert("Marks must total 100");
}

function countMark(mark) {
    var totalMark = 0;
    totalMark += parseInt(mark)
    return totalMark;
}

function doAdd() {
    var taskid = document.getElementById("taskid").value;
    var taskname = document.getElementById("taskname").value;
    var taskmark = document.getElementById("taskmark").value;

    if (taskid.length === 0)
        window.alert("Task Id cannot be empty!");
    if (taskname.length === 0)
        window.alert("Task name cannot be empty!");
    if (taskmark.length === 0)
        window.alert("Task Mark cannot be empty!");
    else if (!markpattern.test(taskmark))
        window.alert("Invalid data in mark field");

    var marks = parseInt(document.getElementById("taskmark"));
    if (marks < 0 || marks > 100)
        window.alert("Marks out of range. Please re-enter");
    countMark(marks);
}

My question is when i keep call the doAdd() function. my marks will keep adding . want to do like passing reference like in C++ . my function countMark(...) will keep adding .

after that, when my form submitted, my form will call the function checkData() If my totalmark is not 100 . will prompt out the alert and error.

but my code is not working . I guess that my countMark function wrong somewhere

rab
  • 4,134
  • 1
  • 29
  • 42
Felicia Tan
  • 1,547
  • 2
  • 12
  • 9
  • you are reassign 0 to total mark `var totalMark = 0;`, and `totalMark` inside scope of `countMark`. try like `if( !window.totalMark ) window.totalMark = 0;` – rab Jun 15 '13 at 08:38
  • Can you provide a [jsfiddle](http://jsfiddle.net) of this? – Niccolò Campolungo Jun 15 '13 at 08:39
  • @LightStyle please make a jsfiddle .. I will correct it for you .. I have less time – rab Jun 15 '13 at 08:40
  • How can I make a fiddle of this? There are too much elements missing, like the `markpattern` value. I wasn't referring to your comment, but I was asking to @FeliciaTan – Niccolò Campolungo Jun 15 '13 at 08:41
  • Either you forgot a { } in your last if, or you indentation is misleading. – GameAlchemist Jun 15 '13 at 08:43
  • http://jsfiddle.net/8UcJs/ here it's .when u click define task . my totalmark seem like didn't run – Felicia Tan Jun 15 '13 at 09:02
  • @FeliciaTan Just saw your fiddle: you cannot use `onclick="doAdd()"` in a fiddle (at least that's what I experienced). Give your element an Id (for instance "addButton") and add the event handler at the top of your javaScript code: `document.getElementById("addButton").onclick = doAdd;` – basilikum Jun 15 '13 at 09:40

2 Answers2

0

If I understand you correctly, you're looking for the equivalent of a static variable - something that gets initialized the first time the function is called, and keeps it's value for subsequent calls.

Take a look at this related question: https://stackoverflow.com/a/1535650/2444111

The top answer (by CMS) is talking about class-based static variables, which are not quite the same thing.

The second answer (by Pascal MARTIN) is what you're looking for. It takes advantage of the fact that JS functions are also objects, and stores the variable as a property of the function object. This is a better solution than using a global variable (or a property of window, which is what a global actually is)

Community
  • 1
  • 1
jcsanyi
  • 8,133
  • 2
  • 29
  • 52
0

There are several issues in your code and it's really hard to say what your intention was. But I will address what I found.

In the following piece of code you are requesting a DOM Element and try to parse it as an Integer. The result of that type convertion is always NaN. Maybe wanted to get the value attribute of your element, like you did before. (Also, don't request the same element multiple times. Request it once, save the result in a variable and use that variable from that on).

var marks = parseInt(document.getElementById("taskmark"));
if (marks < 0 || marks > 100)
    window.alert("Marks out of range. Please re-enter");
countMark(marks);

Your function countMark is pretty useless, because it will always return whatever Number you pass to it (see comments in your code).

function countMark(mark) {
    var totalMark = 0; //create a new variable with value 0
    totalMark += parseInt(mark)  //add "mark" to that variable
    return totalMark;  //return that variable => 0 + mark = mark (and if mark = NaN => 0 + mark = NaN)
}

Maybe you wanted to make totalMark a global variable, than you would need to define it outside of your function:

var totalMark = 0;
function countMark(mark) {
    totalMark += parseInt(mark);
    return totalMark;
}

Last but not least, lets analyse your function checkData:

function checkData() {
    var temp = 0;  //create a local variable with value 0
    var totalMarks = countMark(temp);  //pass 0 to countMark => return 0 => totalMarks = 0
    if (totalMarks != 100) //always true since totalMarks is always 0
        window.alert("Marks must total 100"); //will always alert
}
basilikum
  • 10,378
  • 5
  • 45
  • 58