i trying generate golf scoring system. in system want compare amount of points user have against ideal sum of points.
the ideal sum of points each hole 2. when user after 1'st hole has 2 points return should 0. if user on next hole makes 3 points return should 1. if makes 1 point should -1.
some how can't work!?!?
here 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; (var l = 0; l < n + 1; l++) { totalpoints += myobject[l]; } //alert("totalpoints: "+totalpoints); // adding points each hole current ideal point // var x = n; var idealpoints = (x * 2) + 2; // comparing ideal points players actual points // var escore = totalpoints - idealpoints; $('#tpoints').html(escore); $('#theobject').html(json.stringify(myobject, null, 4)); }); my trouble seems come this:
for (var l = 0; l < n + 1; l++) { totalpoints += myobject[l]; } i thinks have tried everything, can't solve this.
a jsfiddle example here: http://jsfiddle.net/jmansa/atkvp/
hoping , in advance :-)
your loop isn't summing values because values have been converted strings split() method.
i'm having trouble understanding script supposed do, if change
var r = thisnumber[1]; to
var r = parseint(thisnumber[1]); r integer added totalpoints opposed being concatenated string
further, you're getting undefined values when values of object haven't been defined yet. in loop, should this:
if(myobject[l]) { totalpoints += myobject[l]; } i have applied changes fiddle
Comments
Post a Comment