var teamsFirst = [ { name: "Barcelona", country: "Spain" }, { name: "Real Madrid", country: "Spain" }, { name: "MU", country: "England" }, { name: "Totenham", country: "England" } ]; var teamsSecond = [ { name: "Atletico Madrid", country: "Spain" }, { name: "Valencia", country: "Spain" }, { name: "Chelsea", country: "England" }, { name: "Arsenal", country: "England" } ]; var groupA = []; var groupB = []; var groupC = []; var groupD = []; var groups = [ groupA, groupB, groupC, groupD ]; function compareRandom() { return Math.random() - 0.5; } function printTeams() { for ( let i = 0; i < groups.length; i += 1 ) { console.log('Group ' + i + ": " + groups[i][0].name + " and " + groups[i][1].name); } } function checkTeams() { var response = false; for ( var i = 0; i < groups.length; i += 1 ) { if ( groups[i][0].country === groups[i][1].country ) { response = true; } } return response; } function cleanGroups() { groupA = []; groupB = []; groupC = []; groupD = []; } for ( var j = 0; j < 100; j++ ) { teamsFirst.sort(compareRandom); teamsSecond.sort(compareRandom); for ( let i = 0; i < groups.length; i += 1 ) { groups[i].push(teamsFirst[i]); groups[i].push(teamsSecond[i]); } var answer = checkTeams(); if (!answer) { printTeams(); break; } else { cleanGroups(); } } 

The task of the program is to sort the teams into groups so that in the same group there are no teams from the same country. The problem is this: if the last for replaced with while , then the program will run infinitely, and the commands will never be displayed in the console. But, if for leave, they sometimes appear. How to fix the program so that the teams are sorted, then check (are there any teams in the same group with the same countries), and in the end - output?

  • Need to distribute teams into groups, without coincidence with the same country? - Let's say Pie
  • Yes that's right. Two teams from the same country cannot be in the same group (note: Chelsea and MJ cannot be in the same group) - Novikov Maxim
  • The code itself works if I use for (it passes 1000 times) and when there are no teams from the same country in the same group — they are output, but not always, since teams may not be correctly scattered over 1000 times. But if you put a while, the code runs indefinitely - Novikov Maxim

2 answers 2

We look through the teams of the first group and for each we look for the first one that is suitable from the second. We remove the suitable ones from the second one so that they are not duplicated in another pair.

 let teamsFirst = [ { name: "Barcelona", country: "Spain" }, { name: "Real Madrid", country: "Spain" }, { name: "MU", country: "England" }, { name: "Totenham", country: "England" } ]; let teamsSecond = [ { name: "Atletico Madrid", country: "Spain" }, { name: "Valencia", country: "Spain" }, { name: "Chelsea", country: "England" }, { name: "Arsenal", country: "England" } ]; let groupA = [], groupB = [], groupC = [], groupD = []; let groups = [ groupA, groupB, groupC, groupD ]; function compareRandom(a, b) { return Math.random() - 0.5; } function printTeams() { for (let i = 0; i < groups.length; i++) { console.log(`Group #${i}: ${groups[i][0].name} (${groups[i][0].country}) VS ${groups[i][1].name} (${groups[i][1].country})`); } } teamsFirst.sort(compareRandom); teamsSecond.sort(compareRandom); for (let f = 0, first; f < teamsFirst.length; f++) { first = teamsFirst[f]; groups[f].push(first); for (let s = 0, second; s < teamsSecond.length; s++) { second = teamsSecond.shift(); if (second.country !== first.country) { groups[f].push(second); break; } teamsSecond.push(second); } } printTeams(); 

     var teamsFirst = [ { name: "Barcelona", country: "Spain" }, { name: "Real Madrid", country: "Spain" }, { name: "MU", country: "England" }, { name: "Totenham", country: "England" } ]; var teamsSecond = [ { name: "Atletico Madrid", country: "Spain" }, { name: "Valencia", country: "Spain" }, { name: "Chelsea", country: "England" }, { name: "Arsenal", country: "England" } ]; var groupA = []; var groupB = []; var groupC = []; var groupD = []; var groups = [ groupA, groupB, groupC, groupD ]; function randomIntegr() { var random = Math.random() * 4; return Math.floor(random); } function randomGroups(teams) { teams.forEach(team =>{ var random = randomIntegr(); if (!groups[random].length){ groups[random].push(team); } else if (groups[random].some(item => {return item.country !== team.country}) && groups[random].length < 2){ groups[random].push(team); } else { for (group of groups){ if (!group.length){ group.push(team); break; } else if (group.some(item => {return item.country !== team.country}) && group.length < 2){ group.push(team); break; } } } }) } randomGroups(teamsFirst); randomGroups(teamsSecond); for (val of groups){ console.log(`${val[0].name} vs ${val[1].name}`); } 

    • The same team falls into more than one group. - Lexx918
    • Thanks, did not notice initially. - Lukas