我非常了解 Java 和 C++。我现在是第一次学习 JavaScript,并且在完成我的一项家庭作业时遇到了麻烦。话虽这么说,我不想要直接的答案,只是指向正确的方向。我需要有两个函数:一个是找到给定数字的索引(从数组中),另一个是获取数组中超过某个值 x 的所有数字。我试过 alert(findIndex(1))、document.write(findIndex(1)),最近我试过使用按钮。除了我创建的按钮,什么都没有显示。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 //EN"
"http://www.w3.org/TR/html4/strict.dtd>
<head>
<script type = "text/javascript">
var a = [0,1,2,3,4];
function findIndex(var c){
for(var count = 0; count< a.length();count++;){
if(a[count] == c){
alert(count);
}
}
alert("No index can be found");
}
function equalOrAboveX(int x){
for(var count = 0; count< a.length();count++;){
if(a[count]>= x){
alert(a[count]);
}
}
}
</script>
</head>
<body>
<input type="button" value="findIndex(1)" onclick="findIndex(1)">
</body>
</html>
最佳答案
length
是属性,不是函数。
count< a.length()
应该是 count < a.length
您的代码中还有其他几个问题。使用 http://www.jshint.com/
我也可以建议你看看 array.filter
和 array.indexOf
.
关于Javascript函数,知识少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19892769/