[英]function return the same value
N_ALPHA = 6;
N_CHOICES = 4;
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var alphabet = ALPHABET.substring(0, N_ALPHA);
var indexForm=new Array();
function guessStrToArr(inStr)
{
{
for (i=0;i<N_CHOICES;i++)
{
indexForm[i]=alphabet.indexOf(inStr.charAt(i).toUpperCase());
}
}
return indexForm;
}
function numBulls(guess, goal)
{
guess=new Array (guessStrToArr(prompt('enter your guess1')));
alert(guess);
goal=new Array(guessStrToArr(prompt('enter your guess2')));
var checkArray = new Array (guess.concat(goal);
alert(checkArray);
var count=0;
for (i=0;i<N_CHOICES;i++)
{
if (guess[i]===goal[i])
{
count++
}
}
return count;
}
numBulls();
when I run the above code, it returnt the same array, evern I type different code into prompt alet box. 当我运行上面的代码时,它返回相同的数组,直到我在提示框内键入不同的代码。 I don't know why, can you please help me? 我不知道为什么,你能帮我吗? Thank you very much. 非常感谢你。
indexForm
is a global variable. indexForm
是一个全局变量。 When you call the guessStrToArr
function for the first time array gets populated. 当您第一次调用guessStrToArr
函数时,将填充数组。 The second time it gets repopulated. 第二次被填充。 When you're doing the comparison, your comparing the array against itself. 比较时,将数组与自身进行比较。 Try this: 尝试这个:
function guessStrToArr(inStr) {
var indexForm = new Array();
for (i = 0; i < N_CHOICES; i++) {
indexForm[i] = alphabet.indexOf(inStr.charAt(i).toUpperCase());
}
return indexForm;
}
Also note that you are making no attempt to ensure that inStr.length
is the same as N_CHOICES
. 还要注意,您没有尝试确保inStr.length
与N_CHOICES
相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.