I have a pretty good understanding of Java and C++. I am now learning JavaScript for the first time and am having trouble with one of my homework assignments. That being said, I do not want the direct answer, just a point in the right direction. I need to have two functions: one to find the index of a given number (from an array), and one to get all numbers of an array over a certain value x. I have tried alert(findIndex(1)), document.write(findIndex(1), and most recently I've tried using a button. Nothing gets displayed, except the button I have created.
<!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>
-
Have you tried your browser's web inspector / Firebug's error console? It should point you in the right direction...Graham– Graham2013年11月10日 17:35:03 +00:00Commented Nov 10, 2013 at 17:35
-
I don't know if this is enough of the the "right direction" but you use breakpoints to debug the code in the chrome development tools (F12) which should help you quickly figure out what is wrong here, or where the wrong ismonastic-panic– monastic-panic2013年11月10日 17:37:03 +00:00Commented Nov 10, 2013 at 17:37
-
Thanks, I'll read up on chrome dev tools to figure out how to use it properly :)Chad– Chad2013年11月10日 17:40:57 +00:00Commented Nov 10, 2013 at 17:40
3 Answers 3
length is a property, not a function.
count< a.length() should be count < a.length
You have several other issues in your code. Use http://www.jshint.com/
May I also suggest you to look at array.filter and array.indexOf.
1 Comment
You have a series of syntax errors..
var candvar xin the function parameter definitions is wrong.. remove thevarlengthis a property and not a function. So remove the()after it..- remove the
;at the end of the loop definitions
All together
var a = [0,1,2,3,4];
function findIndex(c){
for(var count = 0; count< a.length;count++){
if(a[count] == c){
alert(count);
}
}
alert("No index can be found");
}
function equalOrAboveX(x){
for(var count = 0; count< a.length;count++){
if(a[count]>= x){
alert(a[count]);
}
}
}
Comments
Glad to hear you are learning javascript.
A few things. As plalx said length is a property not a function.
Also you don't use the var keyword when declaring arguments for your functions.
In general to find these faults you can use the javascript console in your browser. It can be displayed by using (shift in linux/windows)cmd+alt+j in chrome. It's might also be a good idea to use node.js to run code which does not require a browser.
A common pattern for javascript development that you might want to consider reading up on is. The anonymous self invoking function, it looks like this. Javascript is a lot like C++ in the sense that there are a lot of gotchas and things you'll just have to know.
(function () {
"use strict";
//Code here
}());
Using a linter, which plaxl talked about, of some sort is highly recommended.
Good luck in learning javascript