1

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 :-)

Mansa
  • 2,277
  • 10
  • 37
  • 67

2 Answers2

2

Your for loop isn't summing the values because the values have been converted to strings with the split() method.

I'm having trouble understanding exactly what the script is supposed to do, but if you change

var r = thisNumber[1];

to

var r = parseInt(thisNumber[1]);

r will be an integer that will get added to totalpoints as opposed to being concatenated to it as a string

Further, you're getting undefined values when values of the object haven't been defined yet. In your for loop, you should do this:

if(myObject[l]) {
    totalpoints += myObject[l];
}

I have applied my changes to your fiddle

Willem Ellis
  • 4,886
  • 7
  • 38
  • 55
1

The problem is that your n and r variables are strings, not parsed to numbers. This will lead to

  • an odd loop condition. When n is "1", it will loop l until "1"+1 == "11". That way many zeros and even sum undefineds from non-existing properties will get summed up.
  • The sum itself is flawed. Instead of adding, it will do string concatenation. 0+ "1"+"0"+"0" is "0100", not 1. Also when your concatenating undefined the result will not even be a number. You probably have seen those when alerting the totalpoints.

When you're going to substract that from the idealPoints you get NaN, and that won't be displayed.

To fix it, simply cast n and r to numbers (via unary plus, or use parseInt or something else):

var n = + thisNumber[0];
var r = + thisNumber[1];

(Corrected fiddle)

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375