我'我有问题让这个javascript程序执行
I'm am having issues getting this javascript program to execute
每次我运行程序并输入堆和圈数时,我都会得到一个"TypeError: piles[pileChoice] is undefined"错误。我试过调试它几次,但我仍然不能让它正常工作。
var piles = [
{name: 'Pile A', circles: 'ooooo'},
{name: 'Pile B', circles: 'ooooo'},
{name: 'Pile C', circles: 'ooooo'}
];
function boardPrint(){
console.log("NIM");
for(var i = 0; i < piles.length; i++) {
console.log(piles[i].name + ": " + piles[i].circles);
}
}
function getUserInput(){
return prompt("Enter the letter for the pile (A-C) and the number of stones you want to remove (1-5). Example: A3").toLowerCase();
}
function userMove(){
var pileIdx = 0;
var valid = false;
var numToRemove = 0;
while(!valid) {
var gameIns = getUserInput(); // This will now get called multiple times until user enters valid input
var pileChoice = gameIns[0]; // This makes 'A' turn into 'a', which makes further logic easier.
// I rebuilt this part of the function to be a bit cleaner and to show you how switch statements could be used
switch(pileChoice){
case 'a':
pileIdx = 0;
valid = true;
break;
case 'b':
pileIdx = 1;
valid = true;
break;
case 'c':
pileIdx = 2;
valid = true;
break;
default:
alert('Error! Invalid input.');
}
numToRemove = Math.min(gameIns[1],piles[pileChoice].circles.length); // This way, they can't select a number that is greater than the number remaining in the pile.
}
piles[pileIdx].circles = piles[pileIdx].circles.slice(numToRemove);
}
function computerMove(move){
// Task 1: pick a pile
var pileIdx = 0;
if(piles[0].circles.length > 0) { // tests for whether there are circles left in pile A
piles[0].circles = piles[0].circles.slice(pileIdx); // do something
} else if(piles[1].circles.length > 0) {
piles[1].circles = piles[1].circles.slice(pileIdx); // do something
} else if(piles[2].circles.length > 0) {
piles[2].circles = piles[2].circles.slice(pileIdx); // do something
}
// Task 2: pick a number to remove from the pile
// Optional: see how many piles are left and base your number to remove on that
//var pilesCount = 0;
// [some logic for counting piles]
// Otherwise, just remove all that are remaining from a pile
//var numToRemove = 0;
if (pilesCount > 1){
// select a number to remove
}
else {
// select another number to remove
}
piles[pileIdx].circles = piles[pileIdx].circles.slice(numToRemove);
}
while(true) {
boardPrint();
userMove();
if (boardEmpty() === true) {
boardPrint();
console.log("You win!");
break;
}
boardPrint();
computerMove();
if (boardEmpty() === true) {
boardPrint();
console.log("Computer wins!");
break;
}
}
function boardEmpty() {
// Check if the board is empty
}
在您的userMove
函数中,在while循环结束时,您尝试使用以下行设置numToRemove:
numToRemove = Math.min(gameIns[1],piles[pileChoice].circles.length); // This way, they can't select a number that is greater than the number remaining in the pile.
但是pileschoice是'a', 'b'或'c',对吗?所以你在开关中计算了pileIdx。您需要使用它,因为您定义堆的方式不能使用'a', 'b'或'c'进行索引。
我认为问题就在这里:
你有var piles = [
{name: 'Pile A', circles: 'ooooo'},
{name: 'Pile B', circles: 'ooooo'},
{name: 'Pile C', circles: 'ooooo'}
];
,然后
numToRemove = Math.min(gameIns[1],piles[pileChoice].circles.length);
我想你的意思是piles[pileIdx]
?pileChoice
将是一个字母,你有一个对象数组,所以你需要访问正确的索引。
你试图引用piles[pileChoice]
像桩['a'],桩['b']等在gameIns[0],但桩数组不是以这种方式格式化,因为桩是一个对象数组。
您可以尝试重新考虑您的模型堆的方式,或尝试像下面这样的解决方案来获得circles.length:
// define pileCircleCount
var pileCircleCount;
// go over your piles array
piles.forEach(function(pile) {
// get the pile name value and retrieve the last char which is A/B/C
// and convert it to lowercase
var pileId = pile.name.substr(pile.name.length - 1).toLowerCase();
// now match it up with the pileChoice, if a match is found get its
// circle length and set it to the pileCircleCount
if(pileId === pileChoice) {
pileCircleCount = pile.circles.length;
}
});
numToRemove = Math.min(gameIns[1], pileCircleCount);
相关文章:
- 为什么我的javascript程序没有将其转换为整数而不是字符串
- 为什么Javascript程序员更喜欢在代码中重复字符串作为常量,而不是使用枚举类型的对象
- 基本的HTML Javascript程序.这是怎么回事
- 如何重复Javascript程序
- 我没有'我不明白“;temp<<=y"在这个javascript程序中执行
- Javascript程序,如果语句有困难
- JavaScript程序,随机生成一百次个位数(0,1,…9),并输出生成了多少个0s、1s、2s……9s
- 可以编写一个预先加载所有图像和视频的JavaScript程序
- 这个JavaScript程序是如何工作的
- JavaScript 程序中的错误
- 编写一个JavaScript程序来计算闰年
- 将 JavaScript 程序从 JSON 转换为 XML
- 基本的Javascript程序,需要帮助
- 如何结束 JavaScript 程序中间程序
- 为什么 JavaScript 程序无法运行
- JavaScript 程序中的多个文件
- 如何重置我的 Javascript 程序
- 在javascript程序中使用c动态链接库
- 如何让此 JavaScript 程序根据提示的输入向对象属性发出警报
- 购物清单 Javascript 程序列表项的文本格式不正确