我正在编写一个模拟孩子的足球,棒球,棒球的程序;任何团队比赛。阵列中有10个孩子。每个孩子都有一个随机的幂数。您知道您总是首先选择最好的孩子,然后依此类推。因此,我的脚本为孩子的能力生成了一个随机数。然后对数组进行排序,以确定两个孩子的最高能力,并将它们放在数组的末尾,然后将它们弹出到两个新的数组中,称为teamOne和teamTwo。然后他们扔硬币,看谁先选,差不多是通过if-else循环,在if中嵌套了一会儿。
我的排序工作正常,两个团队的队长弹出两个新的团队数组也是如此。我抛硬币很有效,但后来我茫然。因此,我需要针对此问题的指导或指导材料建议?
这是我的代码,谢谢。
// generate a random number
let getRandNum = function (start, range) {
let getRand = (Math.random() * range) + start;
while (getRand > range) {
getRand = (Math.random() * range) + start;
};
return getRand;
};
// array of 10 kids
var Kids = [{
name: "Bobby",
random: getRandNum(1, 10)
},
{
name: "Frankie",
random: getRandNum(1, 10)
},
{
name: "Juan",
random: getRandNum(1, 10)
},
{
name: "Sid",
random: getRandNum(1, 10)
},
{
name: "Ellie",
random: getRandNum(1, 10)
},
{
name: "Harry",
random: getRandNum(1, 10)
},
{
name: "Chester",
random: getRandNum(1, 10)
},
{
name: "Lucio",
random: getRandNum(1, 10)
},
{
name: "Nancy",
random: getRandNum(1, 10)
},
{
name: "Kim",
random: getRandNum(1, 10)
}
];
// sort based on random number with thanks to:
// https://www.sitepoint.com/sort-an-array-of-objects-in-javascript
function compareValues(key, order = 'asc') {
return function innerSort(a, b) {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
// property doesn't exist on either object
return 0;
}
const varA = (typeof a[key] === 'string') ?
a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string') ?
b[key].toUpperCase() : b[key];
let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order === 'desc') ? (comparison * -1) : comparison
);
};
}
console.log(Kids.sort(compareValues('random', )));
const teamOne = Kids.pop();
const teamTwo = Kids.pop();
console.log(teamOne);
console.log(teamTwo);
console.log(Kids);
let coinToss = "Heads"
if (Math.floor((Math.random() * 2) + 1) == 2) {
coinToss = "Tails";
};
console.log(coinToss);
if (coinToss = "Heads") {
let x = 4;
while (x > 0) {
teamOne.push([Kids.pop()]);
teamTwo.push([Kids.pop()]);
x -= x;
};
} else {
let x = 4;
while (x > 0) {
teamTwo.push([Kids.pop()]);
teamOne.push([Kids.pop()]);
x -= x;
};
console.log("_____________");
console.log(coinSide);
console.log("_____________");
console.log(teamOne.name);
console.log("_____________");
console.log(teamTwo.name);
};
答案 0 :(得分:1)
问题是您没有将teamOne和teamTwo视为数组,因此不能使用“ push”。最重要的是,Kids数组对象具有两个值-“ name”和“ random”-因此,使用“ push”将添加一个新的kid作为对象。
因此,首先,您需要将teamOne和teamTwo定义为数组而不是字符串。其次,您需要从Kids对象中提取“ name”属性(假设您仅出于排序目的使用“ random”,而不再需要它的值)。最后,您还没有为每个团队分配名称,如果您为每个团队使用一个数组,现在就可以这样做。
尝试类似的东西:
// generate a random number
let getRandNum = function (start, range) {
let getRand = (Math.random() * range) + start;
while (getRand > range) {
getRand = (Math.random() * range) + start;
};
return getRand;
};
// array of 10 kids
var Kids = [{
name: "Bobby",
random: getRandNum(1, 10)
},
{
name: "Frankie",
random: getRandNum(1, 10)
},
{
name: "Juan",
random: getRandNum(1, 10)
},
{
name: "Sid",
random: getRandNum(1, 10)
},
{
name: "Ellie",
random: getRandNum(1, 10)
},
{
name: "Harry",
random: getRandNum(1, 10)
},
{
name: "Chester",
random: getRandNum(1, 10)
},
{
name: "Lucio",
random: getRandNum(1, 10)
},
{
name: "Nancy",
random: getRandNum(1, 10)
},
{
name: "Kim",
random: getRandNum(1, 10)
}
];
// sort based on random number with thanks to:
// https://www.sitepoint.com/sort-an-array-of-objects-in-javascript
function compareValues(key, order = 'asc') {
return function innerSort(a, b) {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
// property doesn't exist on either object
return 0;
}
const varA = (typeof a[key] === 'string') ?
a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string') ?
b[key].toUpperCase() : b[key];
let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order === 'desc') ? (comparison * -1) : comparison
);
};
}
console.log(Kids.sort(compareValues('random', )));
let teamOne = [];
let teamTwo = [];
let kidName1 = {};
let kidName2 = {};
kidName1 = Kids.pop();
teamOne[0] = kidName1.name;
kidName2 = Kids.pop();
teamTwo[0] = kidName2.name;
teamOne.name = "Team 1";
teamTwo.name = "Team 2";
console.log(teamOne);
console.log(teamTwo);
console.log(Kids);
let coinToss = "Heads"
if (Math.floor((Math.random() * 2) + 1) == 2) {
coinToss = "Tails";
};
console.log(coinToss);
for (let i = 0; i < 4; i++) {
kidName1 = Kids.pop();
kidName2 = Kids.pop();
if (coinToss == "Heads") {
teamOne.push(kidName1.name);
teamTwo.push(kidName2.name);
} else {
teamOne.push(kidName2.name);
teamTwo.push(kidName1.name);
}
};
console.log("_____________");
console.log(coinToss);
console.log("_____________");
console.log(teamOne.name);
console.log("_____________");
console.log(teamTwo.name);
console.log("_____________");
console.log(teamOne.toString());
console.log("_____________");
console.log(teamTwo.toString());