2016-08-14 5 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 = 

私はtotalGoalsStateteams配列のプレイヤーからの累積プレーヤーの目標を等しく持っている簡単な方法が必要です。また、playerThreeに、philsamなどの新しいプレーヤーの1つを、虎やパンテオンのようなチームの1つに記入するにはどうすればいいですか?あなたが必要な場合はここで合計値すべて

+0

あなたのコンストラクタ 'locTeams'は、5つのパラメータを必要とするために、あなたはあなたがthis.playerThree = playerThree''そこにも欠場4を渡しています。 –

+1

@Aleksandardookić - それは本当に* 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

アレイ内のすべてのチームの目標を追加するための一つの方法です他の2人が既にいる場所に追加してください。各プレイヤーは合計目標に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; 
 
} 
 

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

関連する問題