2016-08-14 59 views
-1
function player(name, goals, games) { 
    this.name = name; 
    this.goals = goals; 
    this.games = games; 
} 

var ricky = new player('Ricky', 7, 15); 
var tom = new player('Tom', 15, 17); 
var phil = new player('Phillip', 9, 14); 
var jerry = new player('Jerry', 11, 15); 
var randy = new player('Randy', 4, 16); 
var sam = new player('Sam', 5, 11); 


function locTeams(name, town, wins, playerOne, playerTwo, playerThree) { 
    this.name = name; 
    this.town = town; 
    this.wins = wins; 
    this.playerOne = playerOne; 
    this.playerTwo = playerTwo; 
    this.playerThree = playerThree; 
} 

var tigers = new locTeams('Great Tigers', 'Clayton', 9, ricky, tom); 
var pantheon = new locTeams('The Pantheons', 'Brookedale', 8, jerry, randy); 

teams = [tigers, pantheon]; 

var totalGoalsState = 

内对象的单个属性的值,我需要一个简单的方法有totalGoalsState等于从teams阵列中的玩家累计游戏者目的。另外,我如何填写playerThree与其他新玩家之一,如philsam成为像老虎或万神殿之类的球队之一。总计所有对象

+0

您的构造函数'locTeams'需要5个参数,要传递4.您错过有作为'this.playerThree = playerThree'。 –

+1

@AleksandarĐokić - 它不需要* 5个参数,它*可以有* 5个参数。 – adeneo

+0

@ adeneo是的,但他没有在方法中的任何地方设置“playerThree”...他应该,因为它在构造函数中。 –

回答

1

这里的加起来的目标在阵列,迭代所有球队的一种方式,并减少对每个球员的价值,每队等

function player(name, goals, games) { 
 
    this.name = name; 
 
    this.goals = goals; 
 
    this.games = games; 
 
} 
 
function player(name, goals, games) { 
 
    this.name = name; 
 
    this.goals = goals; 
 
    this.games = games; 
 
} 
 

 
var ricky = new player('Ricky', 7, 15); 
 
var tom = new player('Tom', 15, 17); 
 
var phil = new player('Phillip', 9, 14); 
 
var jerry = new player('Jerry', 11, 15); 
 
var randy = new player('Randy', 4, 16); 
 
var sam = new player('Sam', 5, 11); 
 

 

 
function locTeams(name, town, wins, playerOne, playerTwo, playerThree) { 
 
    this.name = name; 
 
    this.town = town; 
 
    this.wins = wins; 
 
    this.playerOne = playerOne; 
 
    this.playerTwo = playerTwo; 
 
    this.playerThree = playerThree; 
 
} 
 

 
var tigers = new locTeams('Great Tigers', 'Clayton', 9, ricky, tom); 
 
var pantheon = new locTeams('The Pantheons', 'Brookedale', 8, jerry, randy); 
 

 
var teams = [tigers, pantheon]; 
 

 
function getGoals(team) { 
 
\t \t return Object.keys(team).map(function(k) { 
 
    \t return k.indexOf('player') === 0 ? 
 
      team[k] && "goals" in team[k] ? team[k].goals : 0 : 0; 
 
    }).reduce(function(a,b) { return a+b }); 
 
} 
 

 
var totalGoalsState = teams.reduce(function(a,b) {return getGoals(a) + getGoals(b)}); 
 

 
console.log(totalGoalsState)

0

如果您需要第三位玩家,只需将其添加到其他两位已经存在的位置。请注意,我正在消除重复项,因为每个玩家只能计入一次总目标。

function player(name, goals, games) { 
 
    this.name = name; 
 
    this.goals = goals; 
 
    this.games = games; 
 
} 
 

 
var ricky = new player('Ricky', 7, 15); 
 
var tom = new player('Tom', 15, 17); 
 
var phil = new player('Phillip', 9, 14); 
 
var jerry = new player('Jerry', 11, 15); 
 
var randy = new player('Randy', 4, 16); 
 
var sam = new player('Sam', 5, 11); 
 

 

 
function locTeams(name, town, wins, playerOne, playerTwo, playerThree) { 
 
    this.name = name; 
 
    this.town = town; 
 
    this.wins = wins; 
 
    this.playerOne = playerOne; 
 
    this.playerTwo = playerTwo; 
 
} 
 

 
var tigers = new locTeams('Great Tigers', 'Clayton', 9, ricky, tom); 
 
var pantheon = new locTeams('The Pantheons', 'Clayton', 9, jerry, randy); 
 

 
teams = [tigers, pantheon]; 
 

 
var totalGoalsState = teams.reduce((p, c) => p.concat([c.playerOne, c.playerTwo]), []) 
 
          .reduce((p, c) => p.includes(c) ? p : p.concat(c), []) 
 
          .reduce((p, c) => p + c.goals, 0); 
 

 
console.log(totalGoalsState);

相关问题