【发布时间】:2020-11-23 05:16:43
【问题描述】:
感谢您在此练习中提前提供的帮助,其中的真相我还没有发现如何解决它
如何动态生成团队之间可能的遭遇?
具有以下输入字段
- 开始日期
- 团队
- 字段
- 玩几天
以以下数据为例
const startDate = "03-08-2020";
const teams = ["A", "B", "C", "D", "E", "F"];
const fields = ["Field1", "Field2"];
const daysToPlay = ["Monday", "Wednesday"];
现在,我需要根据这些数据动态生成比赛日及其比赛匹配,比赛场地必须像从日期到日期的日期一样互换。从 startDate 值开始
输出示例如下
const output = [
{
number: 1,
date: "03-08-2020", // Monday
field: "Field1",
matches: [
["A", "B"],
["C", "D"],
["E", "F"],
],
},
{
number: 2,
date: "05-08-2020", // Wednesday
field: "Field2",
matches: [
["A", "C"],
["B", "E"],
["C", "F"],
],
},
];
以这种方式,根据团队之间唯一可能遇到的次数。
更新 0
- 所有球队必须在每个日期参加比赛
- 团队数量总是偶数
- 比赛在每支球队都与其他球队交手后结束,
更新 1
我遵循 Oliver 的建议,但在最后一组比赛中我得到了一场比赛,这里我期待两场比赛像以前的比赛一样
const teams = ["A", "B", "C", "D"];
const getCombinations = (data) => {
let output = [];
for (let i = 0; i < teams.length - 1; i++) {
for (let j = i + 1; j < teams.length; j++) {
output = [...output, [data[i], data[j]]];
}
}
return output;
};
const getMatches = (data) => {
let matches = [];
let i = 0;
while (data.length) {
for (const [index, entry] of data.entries()) {
if (index === 0) {
matches.push([entry]);
data.splice(index, 1);
continue;
}
const [team1, team2] = entry;
const idx = matches[i].findIndex(
(value) => value.includes(team1) || value.includes(team2)
);
if (idx !== -1) {
continue;
}
matches[i].push(entry);
data.splice(index, 1);
}
i++;
}
return matches;
};
const combinations = getCombinations(teams);
const matches = getMatches(combinations);
console.log(matches);
更新 2
修复之前的更新
const teams = ["A", "B", "C", "D"];
const getCombinations = (data) => {
let output = [];
for (let i = 0; i < teams.length - 1; i++) {
for (let j = i + 1; j < teams.length; j++) {
output = [...output, [data[i], data[j]]];
}
}
return output;
};
const getMatches = (data) => {
let matches = [];
let i = 0;
while (data.length) {
for (const [index, entry] of data.entries()) {
if (data.length === 1) {
matches[i - 1].push(entry);
data.splice(index, 1);
break;
}
if (index === 0) {
matches.push([entry]);
data.splice(index, 1);
continue;
}
const [team1, team2] = entry;
const idx = matches[i].findIndex(
(value) => value.includes(team1) || value.includes(team2)
);
if (idx !== -1) {
continue;
}
matches[i].push(entry);
data.splice(index, 1);
}
i++;
}
return matches;
};
const combinations = getCombinations(teams);
console.log(combinations);
const matches = getMatches(combinations);
console.log(matches);
更新 3
我快到了
我在执行与日期相关的任务时遇到问题,如何获得正确的日期。为了使解决方案更容易,我设法通过一周中的天数而不是日期的名称来更改游戏日的输入,这样
Sunday 0
...
Saturday 6
在示例中,日期对应于Monday (1) 和星期三(3)
感谢您的评论
const startDate = "2020-08-03";
const matchDays = [1, 3];
const fields = ["Field 1", "Field 2"];
const teams = ["A", "B", "C", "D"];
const getCombinations = (data) => {
let output = [];
for (let i = 0; i < teams.length - 1; i++) {
for (let j = i + 1; j < teams.length; j++) {
output = [...output, [data[i], data[j]]];
}
}
return output;
};
const getMatches = (data) => {
let matches = [];
let i = 0;
while (data.length) {
for (const [index, entry] of data.entries()) {
if (data.length === 1) {
matches[i - 1].push(entry);
data.splice(index, 1);
break;
}
if (index === 0) {
matches.push([entry]);
data.splice(index, 1);
continue;
}
const [team1, team2] = entry;
const idx = matches[i].findIndex(
(value) => value.includes(team1) || value.includes(team2)
);
if (idx !== -1) {
continue;
}
matches[i].push(entry);
data.splice(index, 1);
}
i++;
}
return matches;
};
const getGameDays = (data) => {
const options = {
year: "numeric",
month: "2-digit",
day: "2-digit",
};
return data.map((entry, index) => {
return {
number: index + 1,
date:
index === 0
? new Date(startDate).toLocaleDateString("es-ES", options)
: new Date(startDate).toLocaleDateString("es-ES", options), // Here I need to move on every target day of week in matchDays
field:
fields.length === 1
? fields[0]
: index === 0
? fields[0]
: index % 2 === 0
? fields[0]
: fields[1],
matches: [...entry],
};
});
};
const combinations = getCombinations(teams);
console.log(combinations);
const matches = getMatches(combinations);
console.log(matches);
const gameDays = getGameDays(matches);
console.dir(gameDays, { depth: null, color: true });
此时设置每天的开始日期
谢谢
【问题讨论】:
-
请发布您迄今为止为实现这一目标所做的尝试。
-
在这个特殊的任务中,我不知道如何开始。我想如果我知道团队可能组合的数量,我就可以知道每天的日期。但我不知道如何获取这些信息
-
要从您需要数组中的项目组合的地方开始,请关注:stackoverflow.com/questions/43241174/…
-
这里有几项任务要完成,感觉缺少很多约束。我想到的一些问题是:“一个场地每天可以打多少场比赛?”,“比赛将如何安排(锦标赛,循环赛,复赛)?”,“多少场比赛?单支球队可以玩一天吗?”。所以对我来说,为这个问题提供一个真正的答案似乎很不完整。我认为你只会得到一些关于组合学的知识,就是这样。
-
你说得对,我会更新问题
标签: javascript