![](/img/trans.png)
[英]Is there a way to cycle through elements in an array using the onClick function in react
[英]Function call using a for loop to cycle through array not working, Help please
我非常新的JavaScript,但我有我花了一整天来研究和解决问题,喜欢做自己,但我很坚持。
我的代码很简单,我已经创建了5个功能,1个试验字母,一个测试数字,1为一个开放的支架和1闭合支架,第五点上了一个句号。 这些函数返回true。
然后我创建了一个功能需要取决于什么是文本字符串发现产生数字输出时,将调用所有这些功能。
回到顶部,我有一个字符串,我创建采用分体式的每个所述串中的字符放入所述阵列中的单独的地址的数组。 这个想法是,我使用for循环通过阵列和输出的类型成同一性,然后将其以列表输出到滚动。
问题是它会做一次循环,然后崩溃了,我无法找到问题,问题在于行:
identity = isWhat(ModCompound[x],ModCompound[y]);
以//删除它和for循环运行正常。
我想知道为什么? 怎么了
代码:(原始格式)
<!DOCTYPE html>
<!--
Sjb 19/03/2015
-->
<html>
<head>
<title></title>
</head>
<body>
Chemicals
</br>
Compound: <input id="compound" value="NaCl.2(H20)">
<script>
var identity;
var x;
Compound = document.getElementById('compound').value;
ModCompound = Compound;
ModCompound = ModCompound.split('');
for (i = 0; i < Compound.length; i++){
x = i;
y = i;
document.write(i);
identity = isWhat(ModCompound[x],ModCompound[y]);
document.write(Math.random() + " : " + identity + "</p>");
}
ModCompound= ModCompound.join('');
// Custom Functions
function isWhat(n1,c1) //OUTPUT 1,2,9,0,8
{
if (isLetter(n1,c1) === true) {
return isWhat = 1;
//document.write = "L";
} //OUTPUT 1
if (isNumber(n1,c1) === true) {
return isWhat = 2;
//document.write = "N";
} //OUTPUT 2
if (isOpenBracket(n1,c1) === true){
return isWhat = 9;
//document.write("OB");
} //OUTPUT 9
if (isClosedBracket(n1,c1) === true) {
return isWhat = 0;
//document.write = "CB";
} //OUTPUT 0
if (isFullStop(n1,c1) === true) {
return isWhat = 8;
//document.write = "FS";
} //OUTPUT 8
}
function isNumber(n, c) //OUTPUT(s) TRUE
{
if (n >= 0 || n <= 9 ) {
//document.write(n + " N " + c + "</p>");
return isNumber = true;
}
}
function isLetter(n, c)
{
n = n.charCodeAt(0);
if (((n >= 65) && (n <= 90)) || ((n >= 97) && (n <= 122))) {
//document.write(n + " L " + c + "</p>");
return isLetter = true;
}
}
function isOpenBracket(n,c)
{
if (n === "(") {
//document.write(n + " OB " + c + "</p>");
return isOpenBracket = true;
}
}
function isClosedBracket(n,c)
{
if (n === ")") {
//document.write(n + " CB " + c + "</p>");
return isClosedBracket = true;
}
}
function isFullStop(n,c)
{
if (n === ".") {
//document.write(n + " F " + c + "</p>");
return isFullStop = true;
}
}
</script>
</body>
</html>
您的退货声明应如下所示:
return 1;
您正在编写此代码:
return isWhat = 1;
将变量isWhat
设置为1
,然后将其返回。 尽管isWhat
是一个函数,所以将其设置为1
将使您无法调用它(因为现在它是一个数字,而不是一个函数)。 另外,也不需要键入var===true
,因为如果变量为true,则var
将求值为true
,并且if
语句将运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.