第一次发布此版本很糟糕。
我正在尝试创建一种方法来创建,过滤和重建名单作为基于重复的多维数组,并且它的总薪水低于某个工资上限。
我可以构建名单,过滤重复项,并检查总薪水,但无法找出一种方法来过滤重复项和总薪水的两个条件,然后重建,直到两者都满足。
我尝试了一个函数,它将名单和总薪水作为两个参数传递,然后是一个while循环来检查两个参数,以防止重复条件和工资上限的限制 - 但是不能让它工作,只是运行无限循环或根本不检查最终功能。
这是完整的代码......
var SalaryCap = 60000;
var Roster = [];
var QBs = {
DrewBrees : 8000,
MattRyan : 7000,
MatthewStafford : 6000,
TomBrady : 10000,
JayCutler : 6500,
AaronRodgers : 9000,
PhillipRivers : 8500,
TeddyBridgewater : 6000
};
var RBs = {
AdrianPeterson : 9000,
ToddGurley : 9500,
DevontaFreeman : 8500,
FrankGore : 7000,
MattForte : 7500,
ArianFoster : 8000
};
var WRs ={
DeAndreHopkins : 8900,
OdellBeckhamJr : 8800,
AntonioBrown : 8700,
AJGreen : 8300,
DezBryant : 8100,
JulianEdelman : 8000,
CalvinJohnson : 8000,
DemaryiusThomas : 7900,
EmmanuelSanders : 7800,
RandallCobb : 7700,
MikeEvans : 7700,
// AlshonJeffery : 7600,
// AllenRobinson : 7500,
// LarryFitzgerald : 7400,
// AllenHurns : 7300,
// BrandinCooks : 7200,
// AmariCooper : 7100,
// JarvisLandry : 7000,
// StefonDiggs : 6600,
// JordanMatthews : 6600,
// MartavisBryant : 6500,
// JeremyMaclin: 6500,
// WillieSnead : 6500,
// PierreGarcon : 6400
};
var TEs = {
RobGronkowski : 8000,
GregOlsen : 6300,
TylerEifert : 6200,
GaryBarnidge : 6100,
JimmyGraham : 6000,
DelanieWalker : 5900,
JordanReed : 5800,
TravisKelce : 5800,
JasonWitten : 5600,
BenjaminWatson : 5500,
RichardRodgers : 5400
};
var Ks = {
StephenGostkowski : 5100,
CalebSturgis : 5000,
StevenHauschka : 5000,
MasonCrosby : 5000,
BlairWalsh : 4900,
JustinTucker : 4900,
BrandonMcManus : 4800,
JoshBrown : 4800,
ConnorBarth : 4800,
KaiForath : 4700,
ChandlerCantanzaro : 4700
};
var DEFs = {
StLouisRams : 5400,
DenverBroncos : 5200,
CincinattiBengals : 5000,
SeattleSeahawks : 4900,
NewEnglandPatriots : 4900,
GreenBayPackers : 4900,
ArizonaCardinals : 4700,
CarolinaPanthers : 4700,
PhiladelphiaEagles : 4700,
PittsburghSteelers : 4600,
DallasCowboys : 4600,
MinnesotaVikings : 4500,
NewYorkGiants : 4500,
MiamiDolphins : 4500,
BaltimoreRavens : 4400
};
function randomQuarterback(QBs) {
var QB = [];
var arrayOfAllKeys = Object.keys(QBs);
// console.log(arrayOfAllKeys);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
QB.push(objKey, QBs[objKey]);
Roster.push(QB);
}
};
function randomRunningbackOne(RBs) {
RB1 = [];
var arrayOfAllKeys = Object.keys(RBs);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
RB1.push(objKey, RBs[objKey]);
Roster.push(RB1);
}
}
function randomRunningbackTwo(RBs) {
RB2 = [];
var arrayOfAllKeys = Object.keys(RBs);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
RB2.push(objKey, RBs[objKey]);
Roster.push(RB2);
}
}
function randomWideoutOne(WRs) {
WR1 = [];
var arrayOfAllKeys = Object.keys(WRs);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
WR1.push(objKey, WRs[objKey]);
Roster.push(WR1);
}
}
function randomWideoutTwo(WRs) {
WR2 = [];
var arrayOfAllKeys = Object.keys(WRs);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
WR2.push(objKey, WRs[objKey]);
Roster.push(WR2);
}
}
function randomWideoutThree(WRs) {
WR3 = [];
var arrayOfAllKeys = Object.keys(WRs);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
WR3.push(objKey, WRs[objKey]);
Roster.push(WR3);
}
}
function randomTEone(TEs) {
TE1 = [];
var arrayOfAllKeys = Object.keys(TEs);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
TE1.push(objKey, TEs[objKey]);
Roster.push(TE1);
}
}
function randomKicker(Ks) {
K1 = [];
var arrayOfAllKeys = Object.keys(Ks);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
K1.push(objKey, Ks[objKey]);
Roster.push(K1);
}
}
function randomDEF(DEFs) {
DEF1 = [];
var arrayOfAllKeys = Object.keys(DEFs);
// console.log(arrayOfAllKeys);
for(var i=0; i<1; i++){
var rnd = Math.floor(Math.random()*arrayOfAllKeys.length);
var objKey = arrayOfAllKeys[rnd];
DEF1.push(objKey, DEFs[objKey]);
Roster.push(DEF1);
}
}
//////////////////////////////////////////////////
// Roster Compiler & Duplicate Filter
//////////////////////////////////////////////////
function rosterConstruction() {
randomQuarterback(QBs);
randomRunningbackOne(RBs);
randomRunningbackTwo(RBs);
randomWideoutOne(WRs);
randomWideoutTwo(WRs);
randomWideoutThree(WRs);
randomTEone(TEs);
randomKicker(Ks);
randomDEF(DEFs);
for(var i = 0; i < Roster.length; i++) {
for(var j = i + 1; j < Roster.length;) {
if(Roster[i][0] == Roster[j][0] && Roster[i][1] == Roster[j][1])
// Found the same. Remove it.
Roster.splice(j, 1);
else
// No match. Go ahead.
j++;
}
}
};
rosterConstruction();
console.log(Roster);
//////////////////////////////////////////////////
// Salary Cap Check
//////////////////////////////////////////////////
function salaryCheck(currentRoster) {
var i = 0;
var checker = [];
for(i; i < currentRoster.length; i++) {
checker.push(currentRoster[i][1]);
}
var sum = checker.reduce(add, 0);
function add(a, b) {
return a + b;
}
return sum;
};
var totalSalary = salaryCheck(Roster);
console.log(totalSalary);
//////////////////////////////////////////////////
// Final Compiler
//////////////////////////////////////////////////
function doubleCheck(currentRoster, currentSalary) {
while(currentRoster.length < 9 && currentSalary < 60000) {
Roster = [];
console.log(Roster);
rosterConstruction();
console.log(Roster);
salaryCheck(Roster);
console.log(Roster);
}
// return Roster;
};
doubleCheck(Roster, totalSalary);
console.log(Roster);
console.log(totalSalary);
答案 0 :(得分:0)
好的,我误解了这个功能,这是有效的:
function doubleCheck(currentRoster, currentSalary) {
while(currentRoster.length < 9 || currentSalary > SalaryCap) {
Roster = [];
rosterConstruction();
currentRoster = Roster;
currentSalary = salaryCheck(Roster);
console.log(Roster);
console.log(currentSalary);
}
// return Roster;
};
问题在于你没有修改while循环的值,现在逻辑是:repeat this process un till the condition:"the roster has less than 9 players or the salary is more than 60k" is true
所以你将获得一组9人,总薪水低于SalaryCap(60k) 。
但是我会编辑逻辑,因为没有优化,我会在添加函数期间检查播放器是否已经存在,调用类似于checkPlayerExist()
的函数,以及在同一函数调用中添加另一个函数。 />
另外,我不明白你为什么要使用这样的循环来添加玩家:
for(var i=0; i<1; i++)