Skip to content Skip to sidebar Skip to footer

Looping And Adding Together The Values Generated

I have just started learning javascript, and I've hit a bit of a roadblock whilst designing the logic behind a 10 pin bowling scorecard. I would be really grateful if someone could

Solution 1:

In your example above.

Replace this:

Game.prototype.totalScore = function() {
(this.scorecard[0].rollOne + this.scorecard[0].rollTwo)+
(this.scorecard[1].rollOne + this.scorecard[0].rollTwo)+
(this.scorecard[2].rollOne + this.scorecard[0].rollTwo)+
(this.scorecard[3].rollOne + this.scorecard[0].rollTwo)+
(this.scorecard[4].rollOne + this.scorecard[0].rollTwo)+
(this.scorecard[5].rollOne + this.scorecard[0].rollTwo)+
};

With this:

Game.prototype.totalScore = function() {
  var result = 0;
  for (var i = 0; i<6; i++) {
    result += this.scorecard[i].rollOne + this.scorecard[0].rollTwo;
  }
  return result;
};

Solution 2:

  Game.prototype.totalScore = function() {
    total = 0;
    for(i = 0; i < this.scorecard.length; i++)
    {
       total +=this.scorecard[i].rollOne + this.scorecard[0].rollTwo;
    }
    return total;
};

Post a Comment for "Looping And Adding Together The Values Generated"