UNPKG

@cycxllin/cmpt315-a2

Version:

Fulfills CMPT 315 Assignment 2, MacEwan University Winter 2024.

48 lines (39 loc) 1.45 kB
/* Takes array of matches and returns an object which lists each participant and a list of who they've beaten. Requires: participants.js (function which creates a list of participants) Parameters: array of matches where each match is a dictionary containing the following key: values winner: string loser: string loser-points: number Returns: Object consisting of strings of winners (as key) with list of strings of who they have beaten (as value) */ const participants = require('./participants.js'); function winnersObject(matches) { if (Array.isArray(matches)){ const winners = {}; // make list of players and add to winners object as keys players = participants(matches); players.forEach(player => { winners[player] = []; }); // add each loser to appropriate winner if not already there matches.forEach(match =>{ let winner = match.winner; let loser = match.loser; if (winner != undefined && loser != undefined){ if (!winners[winner].includes(loser)){ // add loser to list of players winner has beat winners[winner].push(loser); } } }); return winners; } else { console.error("Error: Parameter passed is not an array"); return -1; } } module.exports = participants;