【发布时间】:2022-01-20 11:30:09
【问题描述】:
我正在编写 Football Team Builder 代码,并希望从多个对象中计算特定属性。在这种情况下,我想统计 5 个足球运动员的国籍。 需要计算来自同一国家的足球运动员的数量,并将其添加到现役球员的技能点总数中。 (见代码)
我在这里阅读了一些关于“Object.keys”的内容,但我对 JavaScript 的了解似乎太低,无法正确使用它。
HTML
<h1> Create your Five-a-side Team </h1>
<p> Formation: <span>1-2-1</span></p>
<p>
Attacker 1: <span id="attacker_1"> </span> <br>
Midfielder 1: <span id="midfielder_1"> </span> <br>
Midfielder 2: <span id="midfielder_2"> </span> <br>
Defender 1: <span id="defender_1"> </span> <br>
Keeper: <span id="keeper"> </span> <br>
</p>
<p>
Total Skill Points: <span id="total_skill_points"> 0</span>★
</p>
<p>
You have <span id="same_nation_count">0</span>
players from <span id="nation_name">the same Nation</span>.
<br>
That means you got <span id="bonus_points"
>0</span> Bonus points added to your Total Skill Points!
</p>
JavaScript
// Attacker 1
const captain_01 = {
firstName: 'Johan',
lastName: 'Cruijff',
skillPoints: 5,
position: 'Attacker',
club: 'FC Barcelona',
nation: 'The Netherlands'
}; captain_01.fullName = `${captain_01.firstName} ${captain_01.lastName}`;
// Midfielder 1
const topclass_01 = {
firstName: 'Frenkie',
lastName: 'de Jong',
skillPoints: 4,
position: 'Midfielder',
club: 'FC Barcelona',
nation: 'The Netherlands'
}; topclass_01.fullName = `${topclass_01.firstName}
${topclass_01.lastName}`;
// Midfielder 2
const talent_01 = {
firstName: 'Ryan',
lastName: 'Gravenberch',
skillPoints: 3,
position: 'Midfielder',
club: 'Ajax',
nation: 'The Netherlands'
}; talent_01.fullName = `${talent_01.firstName}
${talent_01.lastName}`;
// Defender 1
const worldclass_01 = {
firstName: 'Virgil',
lastName: 'van Dijk',
skillPoints: 5,
position: 'Defender',
club: 'Liverpool',
nation: 'The Netherlands'
}; worldclass_01.fullName = `${worldclass_01.firstName}
${worldclass_01.lastName}`;
// Keeper
const keeper_01 = {
firstName: 'Gianluigi',
lastName: 'Donnarumma',
skillPoints: 5,
position: 'Keeper',
club: 'PSG',
nation: 'Italy'
}; keeper_01.fullName = `${keeper_01.firstName}
${keeper_01.lastName}`;
// Active Attacker 1
document.getElementById("attacker_1").innerHTML =
`${captain_01.fullName} (${captain_01.skillPoints}★)
(${captain_01.nation})`;
// Active Midfielder 1
document.getElementById("midfielder_1").innerHTML =
`${talent_01.fullName} (${talent_01.skillPoints}★)
(${talent_01.nation})`;
// Active Midfielder 2
document.getElementById("midfielder_2").innerHTML =
`${topclass_01.fullName} (${topclass_01.skillPoints}★)
(${topclass_01.nation})`;
// Active Defender 1
document.getElementById("defender_1").innerHTML =
`${worldclass_01.fullName} (${worldclass_01.skillPoints}★) (${worldclass_01.nation})`;
// Active Keeper
document.getElementById("keeper").innerHTML =
`${keeper_01.fullName} (${keeper_01.skillPoints}★)
(${keeper_01.nation})`;
// Counts the amount of players from the same nation
let nationCount = '';
// Counts Bonus Points to the Total Skill Points
let bonusPoints = nationCount;
document.getElementById("bonus_points").innerHTML =
bonusPoints;
// Total Skill Points calculator
document.getElementById("total_skill_points").innerHTML =
captain_01.skillPoints + talent_01.skillPoints +
topclass_01.skillPoints + worldclass_01.skillPoints +
keeper_01.skillPoints + bonusPoints;
// Sets name for nation by calculating the most common nation among the players
document.getElementById("nation_name").innerHTML ;
这是我的 JSFiddle:https://jsfiddle.net/u3tL65xz/1/
【问题讨论】:
-
澄清一下,您想计算代表的每个国家吗?您的代码似乎不支持这一点。只有一个
nationCount变量可以增加奖励积分。您的玩家列表中有两个国家。你能澄清一下吗? -
是的!每个国家都需要被计算在内。这个想法是,当有 2 个或更多来自同一国家的玩家时,每个玩家都会增加 1 个额外的奖励积分。因此,在这种情况下,不应计算 1 名来自意大利的球员。
-
好的,看我的回答。在第二个解决方案(withNation)中,您拥有每个国家和计数。我会让你应用积分计算的逻辑。
-
因为你想要国家,我已经编辑了我的答案,所以只有一个解决方案,包括国家和删除的解决方案,只有每个解决方案的计数。如果你想要回来,请告诉我。
标签: javascript object properties