无限制地随机化数组,然后在 Javascript 中将其平均拆分
Randomizing array with no limit, then splitting it up equally in Javascript
var players = [];
var totalplayers = 0
var team1 = [];
var team2 = [];
var commands = {
"teams.join": {
name: "Join random team.",
description: "Anybody who joins will be put to a team",
process: function(bot, msg, suffix) {
players.push(msg.sender);
bot.sendMessage(msg.channel, players);
bot.sendMessage(msg.channel, msg.sender + " has been added to the random team selection.");
totalplayers += 1;
bot.sendMessage(msg.channel, totalplayers)
},
},
"teams.random": {
name: "Random team selection.",
desciption: "Displays all players in random team selection in a random team.",
process: function(bot, msg, suffix) {
var playcount = 0;
bot.sendMessage(msg.channel, "tp: " + totalplayers); // Check
bot.sendMessage(msg.channel, "i: " + playcount); // Check
for (playcount = 0; playcount < totalplayers; playcount++) {
//bot.sendMessage(msg.channel, "Looping?") // Check
var Rteam = players[Math.floor(Math.random() * players.length)];
//bot.sendMessage(msg.channel, Rteam); // Check
if (playcount = 0 || 2 || 4 || 6 || 8) {
team1.push(Rteam);
bot.sendMessage(msg.channel, "isEven = true"); // Check
playcount + 1;
} else if (playcount = 1 || 3 || 5 || 7 || 9) {
team2.push(Rteam);
bot.sendMessage(msg.channel, "isEven = false"); // Check
playcount + 1;
}
playcount + 1;
var roll = players.splice(Rteam, 1);
var yourNumber = roll[totalplayers];
//i += 1;
}
bot.sendMessage(msg.channel, "Team 1: " + team1);
bot.sendMessage(msg.channel, "Team 2: " + team2);
},
}
teams.join
工作正常,但我包括显示teams
的整个部分。 teams.random
应该采用players
数组,随机化数组中的玩家(用户(位置,然后分发它们,以便players[0]
在团队 1 上,players[1]
在团队 2(依此类推(上,直到没有更多的用户。从本质上讲,它是获取一个列表并将其随机分成两组。我在测试时意识到的主要事情是playcount
不会增加(我已经尝试了 For、While 和 Do-till 循环无济于事。
这是
不正确的:
if (playcount = 0 || 2 || 4 || 6 || 8) {
有两个问题:首先,你使用的是=
,这是赋值,而它应该==
进行比较。其次,你不能用这种方式||
与多个元素进行比较,你需要为每个项目单独进行比较。所以它应该是:
if (playcount == 0 || playcount == 2 || playcount == 4 || playcount == 6 || playcount == 8) {
但是如果你想知道playcount
是否均匀,你可以做:
if (playcount % 2 == 0)
您不需要将else if
用于其他测试,只需使用 else
,因为只有两种可能性。
包含以下内容的行:
playcount + 1;
什么都不要做。您根本不需要它,因为您在for()
标头中递增playcount
。
顺便说一下,您的代码中没有任何内容可以阻止您多次选择同一名球员,并将他们添加到不同的团队。
我同意 barmar 对 OP 原始代码的评论。但为了完整起见,这里有一个示例代码,可以将团队分成两半,并将玩家随机放置在每个团队中。
var players = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
team1 = [],
team2 = [],
i;
while (players.length > 0) {
i = Math.floor(Math.random() * players.length);
if (players.length % 2 === 0) {
team1.push(players[i]);
} else {
team2.push(players[i]);
}
players.splice(i, 1);
}
console.log(team1, team2);
输出:
[10, 4, 1, 6, 5] [3, 8, 2, 9, 7]
在浏览器控制台中运行代码几次,您将在每个数组中获得不同的随机数。
相关文章:
- 将字符串拆分为数组时出现JavaScript错误
- 在JavaScript中拆分日期字符串的更好方法是什么
- 如何将字符串拆分为字符,但在javascript中保留空格
- 使用 JavaScript 拆分名字和姓氏
- Clear Case.如何在多行上拆分很长的javascript
- 在javascript中使用split函数希望在页面加载时拆分url
- 拆分javascript字符串以获得所需的值
- 基于多个参数拆分Javascript数组中的值
- 如何在多个文件中拆分 JavaScript 对象定义
- 如何使用 Google 脚本拆分 JavaScript 数组
- 用_拆分Javascript中的字符串
- 什么'这是在Rails应用程序中拆分Javascript或Coffeescript的最佳方式
- 如何拆分javascript&html转换为多个文件
- 如何拆分Javascript和HTML文件
- 用条件拆分JavaScript字符串
- 拆分JavaScript字符串中的最后一个字值
- 在three.js游戏引擎中拆分javascript文件
- 用多个分隔符拆分javascript中的字符串并保留它们
- 需要一个正则表达式来拆分javascript中的字符串
- 如何拆分javascript数组