@cycxllin/cmpt315-a2
Version:
Fulfills CMPT 315 Assignment 2, MacEwan University Winter 2024.
34 lines (29 loc) • 910 B
JavaScript
/*
Takes array of matches and returns an array of all the player names
Parameters: array of matches where each match is a dictionary containing the
following key: values
winner: string
loser: string
loser-points: number
Returns: array consisting of all player names
*/
function participants(matches){
if (Array.isArray(matches)){
let players = [];
matches.forEach(match => {
let winner = match.winner;
let loser = match.loser;
if (winner != undefined && !players.includes(winner)){
players.push(winner)
}
if (loser != undefined && !players.includes(loser)){
players.push(loser)
}
});
return players;
} else {
console.error("Error: Parameter passed is not an array");
return -1;
}
}
module.exports = participants;