卡在我的游戏的 javascript 函数上
Stuck on a javascript function for my game
所以我正在创建一个函数来接受两个参数,
- 第一个参数是人名
- 第二个是他们已经捐了多少钱。
每个人都被分配到一个数组,具体取决于他们期望捐赠多少。变量
中的人
- a预计捐6个,
- b 应该捐赠 5 和
- c 捐出 4 个。
我希望函数 return 个人应该再捐多少,但问题是:无论我在控制台日志中输入什么名字,它总是假定此人来自 var a。我在这里错过了什么吗?
function findDonationRequirement(name, num){
var a = ["shad0vvfax","DarthPolekat","TheSchwartz",
"Johnnie SR91","Brodie", "HolyPaladin", "Kraven", "Dan Solo",
"Khorathian812", "KingWilliam"];
var b = ["Infantry0223","Ru Baruba Maral","Gray JediTim", "Moof Milker",
"Andain", "Cori Starfire", "Nassyy", "Roylas Trebla", "DarthPapirrin",
"MaximusGiganticus", "Wardai", "George3PO", "Revan2017", "Ravishing Dirk",
"dowi", "dogamidstwind", "SpinelessAce", "Devilscut88", "Dagez",
"The Buckster", "ddpf", "WoollyLemurToes", "Emeralthys Phantom 2060095",
"Lightnara1", "JoLy NYC", "Ccube19", "Anectet Heat", "BigBadBoneDaddy",
"Grizzy", "Zanzibar", "Jesse", "BhMojo", "Kuu Raama", "Jay Fury", "N8Dog",
"Whiskytangofoxtrot"];
var c = ["KingofG0nd0r", "Chrome Cobra", "Bad Rongo", "TK421"];
if (a.hasOwnProperty(name)) {return 6 - num}
else if (b.hasOwnProperty(name)) {return 5 - num}
else if (c.hasOwnProperty(name)) {return 4 - num}
else "Not on list";}
console.log(findDonationRequirement("KingofG0nd0r", 3));
而不是 a.hasOwnProperty(name)
使用 a.indexOf(name) > 0
hasOwnProperty
是一个对象方法,用于查找名称为 name
的 属性
indexOf
是一种数组方法,用于查找文字或对象在数组中的位置。当找不到它时 returns -1
。因此,请使用它的值来确定它是否存在于 a
数组中。
更多关于 indexOf
的信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
你应该使用 indexOf()
function findDonationRequirement(name, num) {
var a = ["shad0vvfax", "DarthPolekat", "TheSchwartz",
"Johnnie SR91", "Brodie", "HolyPaladin", "Kraven", "Dan Solo",
"Khorathian812", "KingWilliam"
];
var b = ["Infantry0223", "Ru Baruba Maral", "Gray JediTim", "Moof Milker",
"Andain", "Cori Starfire", "Nassyy", "Roylas Trebla", "DarthPapirrin",
"MaximusGiganticus", "Wardai", "George3PO", "Revan2017", "Ravishing Dirk",
"dowi", "dogamidstwind", "SpinelessAce", "Devilscut88", "Dagez",
"The Buckster", "ddpf", "WoollyLemurToes", "Emeralthys Phantom 2060095",
"Lightnara1", "JoLy NYC", "Ccube19", "Anectet Heat", "BigBadBoneDaddy",
"Grizzy", "Zanzibar", "Jesse", "BhMojo", "Kuu Raama", "Jay Fury", "N8Dog",
"Whiskytangofoxtrot"
];
var c = ["KingofG0nd0r", "Chrome Cobra", "Bad Rongo", "TK421"];
if (a.indexOf(name)>-1) {
return 6 - num
} else if (b.indexOf(name)>-1) {
return 5 - num
} else if (c.indexOf(name)>-1) {
return 4 - num
} else {
return "Not on list";
}
}
console.log(findDonationRequirement("Ccube19", 3));
console.log(findDonationRequirement("Chrome bra", 3));
所以我正在创建一个函数来接受两个参数,
- 第一个参数是人名
- 第二个是他们已经捐了多少钱。
每个人都被分配到一个数组,具体取决于他们期望捐赠多少。变量
中的人- a预计捐6个,
- b 应该捐赠 5 和
- c 捐出 4 个。
我希望函数 return 个人应该再捐多少,但问题是:无论我在控制台日志中输入什么名字,它总是假定此人来自 var a。我在这里错过了什么吗?
function findDonationRequirement(name, num){
var a = ["shad0vvfax","DarthPolekat","TheSchwartz",
"Johnnie SR91","Brodie", "HolyPaladin", "Kraven", "Dan Solo",
"Khorathian812", "KingWilliam"];
var b = ["Infantry0223","Ru Baruba Maral","Gray JediTim", "Moof Milker",
"Andain", "Cori Starfire", "Nassyy", "Roylas Trebla", "DarthPapirrin",
"MaximusGiganticus", "Wardai", "George3PO", "Revan2017", "Ravishing Dirk",
"dowi", "dogamidstwind", "SpinelessAce", "Devilscut88", "Dagez",
"The Buckster", "ddpf", "WoollyLemurToes", "Emeralthys Phantom 2060095",
"Lightnara1", "JoLy NYC", "Ccube19", "Anectet Heat", "BigBadBoneDaddy",
"Grizzy", "Zanzibar", "Jesse", "BhMojo", "Kuu Raama", "Jay Fury", "N8Dog",
"Whiskytangofoxtrot"];
var c = ["KingofG0nd0r", "Chrome Cobra", "Bad Rongo", "TK421"];
if (a.hasOwnProperty(name)) {return 6 - num}
else if (b.hasOwnProperty(name)) {return 5 - num}
else if (c.hasOwnProperty(name)) {return 4 - num}
else "Not on list";}
console.log(findDonationRequirement("KingofG0nd0r", 3));
而不是 a.hasOwnProperty(name)
使用 a.indexOf(name) > 0
hasOwnProperty
是一个对象方法,用于查找名称为 name
indexOf
是一种数组方法,用于查找文字或对象在数组中的位置。当找不到它时 returns -1
。因此,请使用它的值来确定它是否存在于 a
数组中。
更多关于 indexOf
的信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
你应该使用 indexOf()
function findDonationRequirement(name, num) {
var a = ["shad0vvfax", "DarthPolekat", "TheSchwartz",
"Johnnie SR91", "Brodie", "HolyPaladin", "Kraven", "Dan Solo",
"Khorathian812", "KingWilliam"
];
var b = ["Infantry0223", "Ru Baruba Maral", "Gray JediTim", "Moof Milker",
"Andain", "Cori Starfire", "Nassyy", "Roylas Trebla", "DarthPapirrin",
"MaximusGiganticus", "Wardai", "George3PO", "Revan2017", "Ravishing Dirk",
"dowi", "dogamidstwind", "SpinelessAce", "Devilscut88", "Dagez",
"The Buckster", "ddpf", "WoollyLemurToes", "Emeralthys Phantom 2060095",
"Lightnara1", "JoLy NYC", "Ccube19", "Anectet Heat", "BigBadBoneDaddy",
"Grizzy", "Zanzibar", "Jesse", "BhMojo", "Kuu Raama", "Jay Fury", "N8Dog",
"Whiskytangofoxtrot"
];
var c = ["KingofG0nd0r", "Chrome Cobra", "Bad Rongo", "TK421"];
if (a.indexOf(name)>-1) {
return 6 - num
} else if (b.indexOf(name)>-1) {
return 5 - num
} else if (c.indexOf(name)>-1) {
return 4 - num
} else {
return "Not on list";
}
}
console.log(findDonationRequirement("Ccube19", 3));
console.log(findDonationRequirement("Chrome bra", 3));