2016-08-14 89 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);

相關問題