javascript统计两位选手射击环数
时间: 2024-12-02 12:10:56 浏览: 16
在JavaScript中,你可以创建一个简单的函数来模拟两位选手的射击环数统计。首先,你可以定义一个对象数组来存储每个选手的射击结果,然后计算总环数并找出最高分。下面是一个简单的示例:
```javascript
// 定义一个表示射击成绩的对象
function Shooter(name, scores) {
this.name = name;
this.scores = scores;
}
// 统计每位选手的总环数
Shooter.prototype.totalScore = function() {
return this.scores.reduce((total, score) => total + score, 0);
};
// 计算所有选手的总环数和最高分
function compareShooters(shooters) {
let highestScore = 0;
let shootersStats = shooters.map(shooter => ({
name: shooter.name,
totalScore: shooter.totalScore(),
isHighest: shooter.totalScore() > highestScore ? true : false
}));
shootersStats.forEach(s => {
highestScore = s.isHighest ? Math.max(highestScore, s.totalScore) : highestScore;
});
return {
winners: shootersStats.filter(s => s.isHighest),
highestScore
};
}
// 示例数据
const playerA = new Shooter('Player A', [8, 9, 7]);
const playerB = new Shooter('Player B', [10, 5, 12]);
// 比较两位选手
const stats = compareShooters([playerA, playerB]);
console.log(`Player ${stats.winners[0].name} has the highest score of ${stats.highestScore} rings.`);
阅读全文