@cycxllin/cmpt315-a2
Version:
Fulfills CMPT 315 Assignment 2, MacEwan University Winter 2024.
54 lines (44 loc) • 1.62 kB
JavaScript
/*
Finds the player with the most loser points.
If a player is the winner, the loser_points are subtracted, and if a player is a loser,
then the loser_points are added. Find the player with the largest tally of loser_points.
Requires: participants.js (function which creates a list of participants)
Parameters: Parameters: array of matches where each match is a dictionary containing the
following key: values
winner: string
loser: string
loser-points: number
Returns: string of player with the most loser points
*/
const participants = require('./participants.js');
function biggestLoser(matches) {
if (Array.isArray(matches)){
const points = {};
// make list of players and add to points object as keys
players = participants(matches);
players.forEach(player => {
points[player] = 0;
});
// update points for each match
matches.forEach(match =>{
let winner = match.winner;
let loser = match.loser;
let loser_points = match.loser_points;
// update points
points[winner] = points[winner] - loser_points;
points[loser] = points[loser] + loser_points;
});
// Find the biggest loser
let bLoser = players[0]
players.forEach(player => {
if (points[player] > points[bLoser]){
bLoser = player;
}
});
return bLoser;
} else {
console.error("Error: Parameter passed is not an array");
return -1;
}
}
module.exports = biggestLoser;