I am trying to generate an golf scoring system. In this system I want to compare the amount of points the user have against the ideal sum of points.
The ideal sum of points for each hole is 2. So when a user after 1'st hole has 2 points the return should be 0. If the user on the next hole makes 3 points the return should then be 1. If he only makes 1 point it should be -1.
Some how I can't get this to work!?!?
Here is my demoscript:
var myObject = {
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
7: 0,
8: 0
};
$('.clickme').click(function () {
var thisNumber = $(this).attr("data-id");
var thisNumber = thisNumber.split('-');
var n = thisNumber[0];
var r = thisNumber[1];
myObject[n] = r;
var totalpoints = 0;
for (var l = 0; l < n + 1; l++) {
totalpoints += myObject[l];
}
//alert("Totalpoints: "+totalpoints);
// ADDING POINTS FOR EACH HOLE TO GET THE CURRENT IDEAL POINT //
var x = n;
var idealpoints = (x * 2) + 2;
// COMPARING IDEAL POINTS WITH HE PLAYERS ACTUAL POINTS //
var escore = totalpoints - idealpoints;
$('#tpoints').html(escore);
$('#theObject').html(JSON.stringify(myObject, null, 4));
});
My trouble seems to come from this:
for (var l = 0; l < n + 1; l++) {
totalpoints += myObject[l];
}
I thinks I have tried everything, but can't solve this.
A jsfiddle example here: http://jsfiddle.net/jmansa/AtkVp/
Hoping for help and thanks in advance :-)