During development turned out that the draw can end in a way that
a team drawed from the runners-up pot DO NOT have
an eligible opponent in the other pot.
This case is handled and log functions are added to shorten the code.
/*13.Dec.2021. Draw of the UEFA Champions League quarter-finals. This was a joke with a bad software. They had to redraw it. Decided to code a better one. Rules: there are 16 teams, 8 group-winners in one pot and 8 runners-up in another pot. A runner-up team is drawed first. Then it is paired with a group-winner. There are 2 criteria for the opponent: it can not go from the same group it can not have the same nationality. After the pairing, the drawed teams are taken out from their pots before the next pairing. During development turned out that the draw can end in a way that a team drawed from the runners-up pot DO NOT have an eligible opponent in the other pot. This case is handled and log functions are added to shorten the code.*/ /*database: 2 arrays of objects*/ const groupWinners = [ { name: "Manchester City", group: "A", nationality: "English" }, { name: "Liverpool", group: "B", nationality: "English" }, { name: "Ajax Amsterdam", group: "C", nationality: "Dutch" }, { name: "Real Madrid", group: "D", nationality: "Spanish" }, { name: "Bayern Munich", group: "E", nationality: "German" }, { name: "Manchester United", group: "F", nationality: "English" }, { name: "Lille", group: "G", nationality: "French" }, { name: "Juventus", group: "H", nationality: "Italian" } ]; const runnersUp = [ { name: "Paris Saint-Germain", group: "A", nationality: "French" }, { name: "Atletico Madrid", group: "B", nationality: "Spanish" }, { name: "Sporting CP", group: "C", nationality: "Portuguese" }, { name: "Internazionale", group: "D", nationality: "Italian" }, { name: "Benfica", group: "E", nationality: "Portuguese" }, { name: "Villarreal", group: "F", nationality: "Spanish" }, { name: "FC Salzburg", group: "G", nationality: "Austrian" }, { name: "Chelsea", group: "H", nationality: "English" } ]; /*log function */ function log(textStr, groupStr, nationalityStr, endStr, teamObj) { console.log( textStr, teamObj.name, groupStr, teamObj.group, nationalityStr, teamObj.nationality, endStr ); } /*log the teams */ console.log("\nWELCOME TO THE DRAW OF THE UEFA CHAMPIONS LEAGUE QUARTER-FINALS!\n"); console.log("Let's see the teams!\n"); console.log("Group winners:\n"); groupWinners.forEach( team => { log("", " (Group: ", " Nationality: ", ")", team); } ); console.log("\nRunners-up:\n"); runnersUp.forEach( team => { log("", " (Group: ", " Nationality: ", ")", team); } ); console.log("\n\nLet's start the draw!\n"); draw(); function draw() { /*arrays containing teams which can be still drawed and an array for the pairings*/ let groupWinnersToDraw = [...groupWinners]; let runnersUpToDraw = [...runnersUp]; let pairings = []; /*while there are teams to draw */ while (runnersUpToDraw.length > 0) { /*choose a random team from runnersUpToDraw */ let index = Math.floor(Math.random() * (runnersUpToDraw.length)); let teamToFace = runnersUpToDraw[index]; /*updating the runnersUpToDraw array*/ runnersUpToDraw = runnersUpToDraw .slice(0, index) .concat( runnersUpToDraw.slice(index + 1, runnersUpToDraw.length) ); log("\n\nTeam to face:", " (Group: ", " Nationality: ", ")", teamToFace); console.log("\n"); /*selecting the potential opponents */ let potentialOpponents = groupWinnersToDraw.filter( team => (team.group != teamToFace.group && team.nationality != teamToFace.nationality) ); if (potentialOpponents.length == 0) { potentialOpponents.push( `No eligible opponent for ${teamToFace.name}! We have to re-draw!` ); } if (typeof potentialOpponents[0] == "string") { console.log(potentialOpponents[0]); return; } console.log("Potential opponents:\n"); potentialOpponents.forEach( team => log("", " (Group: ", " Nationality: ", ")", team) ); /*choose a random team from potentialOpponents */ let anotherIndex = Math.floor(Math.random() * (potentialOpponents.length)); let opponent = potentialOpponents[anotherIndex]; log("\n\nThe opponent is:", " (Group: ", " Nationality: ", ")", opponent); /*updating the groupWinnersToDraw array*/ let yetAnotherIndex = 0; for (let i = 0; i < groupWinnersToDraw.length; i++) { if (groupWinnersToDraw[i].name == opponent.name) { yetAnotherIndex = i; } } groupWinnersToDraw = groupWinnersToDraw .slice(0, yetAnotherIndex) .concat( groupWinnersToDraw.slice(yetAnotherIndex + 1, groupWinnersToDraw.length) ); if (runnersUpToDraw.length > 0) { console.log("\n\nRemaining group-winners:\n") groupWinnersToDraw.forEach( team => { log("", " (Group: ", " Nationality: ", ")", team); } ); console.log("\nRemaining runners-up:\n"); runnersUpToDraw.forEach( team => { log("", " (Group: ", " Nationality: ", ")", team); } ); } /*save the pairings */ let drawing = []; drawing.push(teamToFace, opponent); pairings.push(drawing); } /*log the draw */ console.log("\nTHE QUARTER-FINALS: \n"); pairings.forEach( pair => { console.log( pair[0].name, " (Group: ", pair[0].group, " Nationality: ", pair[0].nationality, ")", " vs ", pair[1].name, " (Group: ", pair[1].group, " Nationality: ", pair[1].nationality, ")" ); } ); }
/*2021.12.13. Draw of the UEFA Champions League quarter-finals.This was a joke with a bad software. Decided to code a better one.- /*13.Dec.2021. Draw of the UEFA Champions League quarter-finals.
- This was a joke with a bad software. They had to redraw it. Decided to code a better one.
There are 16 teams, 8 group-winners and 8 runners-up.A runner-up team is drawed first from the 8. It is paired with a gruop-winner.There are 2 rules:- Rules: there are 16 teams, 8 group-winners in one pot and 8 runners-up in another pot.
- A runner-up team is drawed first. Then it is paired with a group-winner.
- There are 2 criteria for the opponent:
- it can not go from the same group
- it can not have the same nationality.
After the pairing, the paired teams are taken out of the list before the next draw.*/- After the pairing, the drawed teams are taken out from their pots before the next pairing.
/*database: 2 arrays of objects*/- During development turned out that the draw can end in a way that
- a team drawed from the runners-up pot DO NOT have
- an eligible opponent in the other pot.
- This case is handled and log functions are added to shorten the code.*/
- /*database: 2 arrays of objects*/
- const groupWinners = [
- {
- name: "Manchester City",
- group: "A",
- nationality: "English"
- },
- {
- name: "Liverpool",
- group: "B",
- nationality: "English"
- },
- {
- name: "Ajax Amsterdam",
- group: "C",
- nationality: "Dutch"
- },
- {
- name: "Real Madrid",
- group: "D",
- nationality: "Spanish"
- },
- {
- name: "Bayern Munich",
- group: "E",
- nationality: "German"
- },
- {
- name: "Manchester United",
- group: "F",
- nationality: "English"
- },
- {
- name: "Lille",
- group: "G",
- nationality: "French"
- },
- {
- name: "Juventus",
- group: "H",
- nationality: "Italian"
- }
- ];
- const runnersUp = [
- {
- name: "Paris Saint-Germain",
- group: "A",
- nationality: "French"
- },
- {
- name: "Atletico Madrid",
- group: "B",
- nationality: "Spanish"
- },
- {
- name: "Sporting CP",
- group: "C",
- nationality: "Portuguese"
- },
- {
- name: "Internazionale",
- group: "D",
- nationality: "Italian"
- },
- {
- name: "Benfica",
- group: "E",
- nationality: "Portuguese"
- },
- {
- name: "Villarreal",
- group: "F",
- nationality: "Spanish"
- },
- {
- name: "FC Salzburg",
- group: "G",
- nationality: "Austrian"
- },
- {
- name: "Chelsea",
- group: "H",
- nationality: "English"
- }
- ];
console.log("\n");console.log("WELCOME IN THE DRAW OF THE UEFA CHAMPIONS LEAGUE QUARTER-FINALS!");console.log("\n");/*arrays containing teams which can be still drawed and an array for the pairings*/let groupWinnersToDraw = [...groupWinners];let runnersUpToDraw = [...runnersUp];let pairings = [];/*while there are teams to draw */while (runnersUpToDraw.length > 0) {/*choose a random team from runnersUpToDraw */let index = Math.floor(Math.random()*(runnersUpToDraw.length));let teamToFace = runnersUpToDraw[index];/*updating the runnersUpToDraw array*/runnersUpToDraw = runnersUpToDraw.slice(0, index).concat(runnersUpToDraw.slice(index+1, runnersUpToDraw.length));console.log("Team to face:", teamToFace.name, teamToFace.group, teamToFace.nationality);console.log("\n");/*selecting the potential opponents */let potentialOpponents = groupWinnersToDraw.filter(team =>(team.group != teamToFace.group && team.nationality != teamToFace.nationality)- /*log function */
- function log(textStr, groupStr, nationalityStr, endStr, teamObj) {
- console.log(
- textStr,
- teamObj.name,
- groupStr,
- teamObj.group,
- nationalityStr,
- teamObj.nationality,
- endStr
- );
console.log("Potential opponents:")potentialOpponents.forEach(team => console.log(team.name, team.group, team.nationality));console.log("\n");/*choose a random team from potentialOpponents */let anotherIndex = Math.floor(Math.random()*(potentialOpponents.length));let opponent = potentialOpponents[anotherIndex];console.log("The opponent is:", opponent.name, opponent.group, opponent.nationality);console.log("\n");/*updating the groupWinnersToDraw array*/let yetAnotherIndex = 0;for (let i = 0; i < groupWinnersToDraw.length; i++) {if (groupWinnersToDraw[i].name == opponent.name) {yetAnotherIndex = i;}}groupWinnersToDraw = groupWinnersToDraw.slice(0, yetAnotherIndex).concat(groupWinnersToDraw.slice(yetAnotherIndex+1, groupWinnersToDraw.length));console.log("Remaining group-winners:")groupWinnersToDraw.forEach(team => console.log(team.name));console.log("\n");/*save the pairings */let drawing = [];drawing.push(teamToFace);drawing.push(opponent);pairings.push(drawing);- }
/*log the draw */- /*log the teams */
- console.log("\nWELCOME TO THE DRAW OF THE UEFA CHAMPIONS LEAGUE QUARTER-FINALS!\n");
- console.log("Let's see the teams!\n");
- console.log("Group winners:\n");
- groupWinners.forEach(
- team => {
- log("", " (Group: ", " Nationality: ", ")", team);
- }
- );
- console.log("\nRunners-up:\n");
- runnersUp.forEach(
- team => {
- log("", " (Group: ", " Nationality: ", ")", team);
- }
- );
- console.log("\n\nLet's start the draw!\n");
- draw();
console.log("THE QUARTER-FINALS: ");console.log("");pairings.forEach(pair => {console.log(pair[0].name, pair[0].group, pair[0].nationality, " vs ",pair[1].name, pair[1].group, pair[1].nationality );});- function draw() {
- /*arrays containing teams which can be still drawed and an array for the pairings*/
- let groupWinnersToDraw = [...groupWinners];
- let runnersUpToDraw = [...runnersUp];
- let pairings = [];
- /*while there are teams to draw */
- while (runnersUpToDraw.length > 0) {
- /*choose a random team from runnersUpToDraw */
- let index = Math.floor(Math.random() * (runnersUpToDraw.length));
- let teamToFace = runnersUpToDraw[index];
- /*updating the runnersUpToDraw array*/
- runnersUpToDraw = runnersUpToDraw
- .slice(0, index)
- .concat(
- runnersUpToDraw.slice(index + 1, runnersUpToDraw.length)
- );
- log("\n\nTeam to face:", " (Group: ", " Nationality: ", ")", teamToFace);
- console.log("\n");
- /*selecting the potential opponents */
- let potentialOpponents = groupWinnersToDraw.filter(
- team =>
- (team.group != teamToFace.group &&
- team.nationality != teamToFace.nationality)
- );
- if (potentialOpponents.length == 0) {
- potentialOpponents.push(
- `No eligible opponent for ${teamToFace.name}! We have to re-draw!`
- );
- }
- if (typeof potentialOpponents[0] == "string") {
- console.log(potentialOpponents[0]);
- return;
- }
- console.log("Potential opponents:\n");
- potentialOpponents.forEach(
- team =>
- log("", " (Group: ", " Nationality: ", ")", team)
- );
- /*choose a random team from potentialOpponents */
- let anotherIndex = Math.floor(Math.random() * (potentialOpponents.length));
- let opponent = potentialOpponents[anotherIndex];
- log("\n\nThe opponent is:", " (Group: ", " Nationality: ", ")", opponent);
- /*updating the groupWinnersToDraw array*/
- let yetAnotherIndex = 0;
- for (let i = 0; i < groupWinnersToDraw.length; i++) {
- if (groupWinnersToDraw[i].name == opponent.name) {
- yetAnotherIndex = i;
- }
- }
- groupWinnersToDraw = groupWinnersToDraw
- .slice(0, yetAnotherIndex)
- .concat(
- groupWinnersToDraw.slice(yetAnotherIndex + 1, groupWinnersToDraw.length)
- );
- if (runnersUpToDraw.length > 0) {
- console.log("\n\nRemaining group-winners:\n")
- groupWinnersToDraw.forEach(
- team => {
- log("", " (Group: ", " Nationality: ", ")", team);
- }
- );
- console.log("\nRemaining runners-up:\n");
- runnersUpToDraw.forEach(
- team => {
- log("", " (Group: ", " Nationality: ", ")", team);
- }
- );
- }
- /*save the pairings */
- let drawing = [];
- drawing.push(teamToFace, opponent);
- pairings.push(drawing);
- }
- /*log the draw */
- console.log("
- THE QUARTER-FINALS: \n");
- pairings.forEach(
- pair => {
- console.log(
- pair[0].name,
- " (Group: ",
- pair[0].group,
- " Nationality: ",
- pair[0].nationality,
- ")",
- " vs ",
- pair[1].name,
- " (Group: ",
- pair[1].group,
- " Nationality: ",
- pair[1].nationality,
- ")"
- );
- }
- );
- }
Simulating the UEFA Champions League Draw. Code is long because of the team data.
/*2021.12.13. Draw of the UEFA Champions League quarter-finals.
This was a joke with a bad software. Decided to code a better one.
There are 16 teams, 8 group-winners and 8 runners-up.
A runner-up team is drawed first from the 8. It is paired with a gruop-winner.
There are 2 rules:
it can not go from the same group
it can not have the same nationality.
After the pairing, the paired teams are taken out of the list before the next draw.
*/
/*database: 2 arrays of objects*/
const groupWinners = [
{
name: "Manchester City",
group: "A",
nationality: "English"
},
{
name: "Liverpool",
group: "B",
nationality: "English"
},
{
name: "Ajax Amsterdam",
group: "C",
nationality: "Dutch"
},
{
name: "Real Madrid",
group: "D",
nationality: "Spanish"
},
{
name: "Bayern Munich",
group: "E",
nationality: "German"
},
{
name: "Manchester United",
group: "F",
nationality: "English"
},
{
name: "Lille",
group: "G",
nationality: "French"
},
{
name: "Juventus",
group: "H",
nationality: "Italian"
}
];
const runnersUp = [
{
name: "Paris Saint-Germain",
group: "A",
nationality: "French"
},
{
name: "Atletico Madrid",
group: "B",
nationality: "Spanish"
},
{
name: "Sporting CP",
group: "C",
nationality: "Portuguese"
},
{
name: "Internazionale",
group: "D",
nationality: "Italian"
},
{
name: "Benfica",
group: "E",
nationality: "Portuguese"
},
{
name: "Villarreal",
group: "F",
nationality: "Spanish"
},
{
name: "FC Salzburg",
group: "G",
nationality: "Austrian"
},
{
name: "Chelsea",
group: "H",
nationality: "English"
}
];
console.log("\n");
console.log("WELCOME IN THE DRAW OF THE UEFA CHAMPIONS LEAGUE QUARTER-FINALS!");
console.log("\n");
/*arrays containing teams which can be still drawed and an array for the pairings*/
let groupWinnersToDraw = [...groupWinners];
let runnersUpToDraw = [...runnersUp];
let pairings = [];
/*while there are teams to draw */
while (runnersUpToDraw.length > 0) {
/*choose a random team from runnersUpToDraw */
let index = Math.floor(Math.random()*(runnersUpToDraw.length));
let teamToFace = runnersUpToDraw[index];
/*updating the runnersUpToDraw array*/
runnersUpToDraw = runnersUpToDraw.slice(0, index).concat(runnersUpToDraw.slice(index+1, runnersUpToDraw.length));
console.log("Team to face:", teamToFace.name, teamToFace.group, teamToFace.nationality);
console.log("\n");
/*selecting the potential opponents */
let potentialOpponents = groupWinnersToDraw.filter(team =>
(team.group != teamToFace.group && team.nationality != teamToFace.nationality)
);
console.log("Potential opponents:")
potentialOpponents.forEach(team => console.log(team.name, team.group, team.nationality));
console.log("\n");
/*choose a random team from potentialOpponents */
let anotherIndex = Math.floor(Math.random()*(potentialOpponents.length));
let opponent = potentialOpponents[anotherIndex];
console.log("The opponent is:", opponent.name, opponent.group, opponent.nationality);
console.log("\n");
/*updating the groupWinnersToDraw array*/
let yetAnotherIndex = 0;
for (let i = 0; i < groupWinnersToDraw.length; i++) {
if (groupWinnersToDraw[i].name == opponent.name) {
yetAnotherIndex = i;
}
}
groupWinnersToDraw = groupWinnersToDraw.slice(0, yetAnotherIndex).concat(groupWinnersToDraw.slice(yetAnotherIndex+1, groupWinnersToDraw.length));
console.log("Remaining group-winners:")
groupWinnersToDraw.forEach(team => console.log(team.name));
console.log("\n");
/*save the pairings */
let drawing = [];
drawing.push(teamToFace);
drawing.push(opponent);
pairings.push(drawing);
}
/*log the draw */
console.log("THE QUARTER-FINALS: ");
console.log("\n");
pairings.forEach(pair => {
console.log(pair[0].name, pair[0].group, pair[0].nationality, " vs ",
pair[1].name, pair[1].group, pair[1].nationality );
});
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});
Input data now come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases. Modified the original test case and added a new one.
/*input data come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases*/ const yearlyElectricCosts = (m1 = 0, m2 = 0, m3 = 0, m4 = 0, m5 = 0, m6 = 0, m7 = 0, m8 = 0, m9 = 0, m10 = 0, m11 = 0, m12 = 0 ) => { /* Create an object with 12 keys and the input values*/ const monthlyCosts = { januaryBill : m1, februaryBill : m2, marchBill : m3, aprilBill : m4, mayBill : m5, juneBill : m6, julyBill : m7, augustBill : m8, septemberBill : m9, octoberBill : m10, novemberBill : m11, decemberBill : m12 } /*print the object*/ console.log(monthlyCosts); // Decide on a variable name and sum all the monthly charges const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr, 0); // Return the value of the variable by typing it in after the `return` keyword below console.log(totalCharges.toFixed(2), typeof(totalCharges.toFixed(2))); /*toFixed converts the result to string, we have to convert it back to number*/ return parseFloat(totalCharges.toFixed(2)); }
const yearlyElectricCosts = () => {// Create an object with 12 keys with the correct values- /*input data come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases*/
- const yearlyElectricCosts = (m1 = 0, m2 = 0, m3 = 0, m4 = 0, m5 = 0, m6 = 0, m7 = 0, m8 = 0, m9 = 0, m10 = 0, m11 = 0, m12 = 0 ) => {
- /* Create an object with 12 keys and the input values*/
- const monthlyCosts = {
januaryBill : 43.11,februaryBill : 50.21,marchBill : 48.92,aprilBill : 62.36,mayBill : 54.64,juneBill : 49.30,julyBill : 61.93,augustBill : 68.54,septemberBill : 71.77,octoberBill : 70.28,novemberBill : 59.56,decemberBill : 62.04- januaryBill : m1,
- februaryBill : m2,
- marchBill : m3,
- aprilBill : m4,
- mayBill : m5,
- juneBill : m6,
- julyBill : m7,
- augustBill : m8,
- septemberBill : m9,
- octoberBill : m10,
- novemberBill : m11,
- decemberBill : m12
- }
// Decide on a variable name and add all the monthly charges- /*print the object*/
- console.log(monthlyCosts);
- // Decide on a variable name and sum all the monthly charges
- const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr, 0);
- // Return the value of the variable by typing it in after the `return` keyword below
- console.log(totalCharges.toFixed(2), typeof(totalCharges.toFixed(2)));
- /*toFixed converts the result to string, we have to convert it back to number*/
- return parseFloat(totalCharges.toFixed(2));
- }
const { assert } = require("chai"); describe("Solution", function() { it("should test for something", function() { assert.strictEqual(yearlyElectricCosts(43.11, 50.21, 48.92, 62.36, 54.64, 49.30, 61.93, 68.54, 71.77, 70.28, 59.56, 62.04), 702.66) }); it("should test for something", function() { assert.strictEqual(yearlyElectricCosts(43.11, 50.21, 48.92, 62.36, 54.64, 49.30, 61.93, 68.54, 71.77, 70.28, 59.56), 640.62) }); });
- const { assert } = require("chai");
- describe("Solution", function() {
- it("should test for something", function() {
assert.strictEqual(yearlyElectricCosts(), 702.66)- assert.strictEqual(yearlyElectricCosts(43.11, 50.21, 48.92, 62.36, 54.64, 49.30, 61.93, 68.54, 71.77, 70.28, 59.56, 62.04), 702.66)
- });
- it("should test for something", function() {
- assert.strictEqual(yearlyElectricCosts(43.11, 50.21, 48.92, 62.36, 54.64, 49.30, 61.93, 68.54, 71.77, 70.28, 59.56), 640.62)
- });
- });
const yearlyElectricCosts = () => { // Create an object with 12 keys with the correct values const monthlyCosts = { januaryBill : 43.11, februaryBill : 50.21, marchBill : 48.92, aprilBill : 62.36, mayBill : 54.64, juneBill : 49.30, julyBill : 61.93, augustBill : 68.54, septemberBill : 71.77, octoberBill : 70.28, novemberBill : 59.56, decemberBill : 62.04 } // Decide on a variable name and add all the monthly charges const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr, 0); // Return the value of the variable by typing it in after the `return` keyword below console.log(totalCharges.toFixed(2), typeof(totalCharges.toFixed(2))); /*toFixed converts the result to string, we have to convert it back to number*/ return parseFloat(totalCharges.toFixed(2)); }
- const yearlyElectricCosts = () => {
// Create 11 more variables with the correct values- // Create an object with 12 keys with the correct values
- const monthlyCosts = {
- januaryBill : 43.11,
- februaryBill : 50.21,
- marchBill : 48.92,
- aprilBill : 62.36,
- mayBill : 54.64,
- juneBill : 49.30,
- julyBill : 61.93,
- augustBill : 68.54,
- septemberBill : 71.77,
- octoberBill : 70.28,
- novemberBill : 59.56,
- decemberBill : 62.04
- }
- // Decide on a variable name and add all the monthly charges
const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr);- const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr, 0);
- // Return the value of the variable by typing it in after the `return` keyword below
return +totalCharges.toFixed(2);- console.log(totalCharges.toFixed(2), typeof(totalCharges.toFixed(2)));
- /*toFixed converts the result to string, we have to convert it back to number*/
- return parseFloat(totalCharges.toFixed(2));
- }