I started about an hour to try to learn JavaScript for the first time. I have a good knowledge of HTML and PHP, which I find is helping. But I can't figure out what's wrong with this. Can someone please correct it for me?
<script type="text/javascript">
function GetArray() {
var Item1 = prompt("Enter list item 1");
Item1 += "\n";
var Item2 = prompt("Enter list item 2");
Item2 += "\n";
var Item3 = prompt("Enter list item 3");
Item3 += "\n";
var List = new Array(Item1, Item2, Item3);
}
GetArray();
for (i = 0; i < List.length; i++) {
return List[i];
}
</script>
-
1Hard to correct it when you haven't even explained what it does or what's wrong with it don't you think?Clive– Clive2015年09月05日 12:17:43 +00:00Commented Sep 5, 2015 at 12:17
-
"List" is local variable. remove "var" keyword to make it global and the it can work. btw, I dont understand why you are trying to call "return" inside for loop. it seems it is not in a function. nonsense to call "return" in thereAdem– Adem2015年09月05日 12:18:32 +00:00Commented Sep 5, 2015 at 12:18
-
I'm trying to have it ask for a text input for each variable, then print it out on the screen. Like I said, I've only been learning JS for an hour, so it's pretty basic stuff.JaceG– JaceG2015年09月05日 12:21:17 +00:00Commented Sep 5, 2015 at 12:21
-
You really need to do some more reading first. Got help me for including a link to W3Schools: w3schools.com/js/js_loop_for.asp Note: The sample code on that page (like yours) doesn't actually do anything with the data, but if you click Try It Yourself you will see code that shows you how to display the data.Mike– Mike2015年09月05日 12:22:56 +00:00Commented Sep 5, 2015 at 12:22
5 Answers 5
Return only makes sense inside a function.You are using return inside a for loop that causes
SyntaxError: Illegal return statement
If you want to print values in List then use
console.log(List[i]);
use it inside the function otherwise it will give a error
ReferenceError: List is not defined
I have made some changes
function GetArray() {
var Item1 = prompt("Enter list item 1");
var Item2 = prompt("Enter list item 2");
var Item3 = prompt("Enter list item 3");
var List = new Array(Item1, Item2, Item3);
for(i=0;i<List.length;++i){console.log(List[i]);}
}
GetArray();
The output is
Comments
First you didn't assign any value to your prompt variables. Like you have to write like this:
syntax- prompt("Your Text","");
and the right code for this is '
<script>
function GetArray() {
var Item1 = prompt("Enter list item 1","");
var Item2 = prompt("Enter list item 2","");
var Item3 = prompt("Enter list item 3","");
var List = new Array(Item1,Item2,Item3);
for (i = 0; i < List.length; i++) {
document.write( List[i]);
}
}
GetArray();
</script>
Hope you know about Local and Global Variables.
1 Comment
<script type="text/javascript">
function GetArray() {
var Item1 = prompt("Enter list item 1");
Item1 += "\n";
var Item2 = prompt("Enter list item 2");
Item2 += "\n";
var Item3 = prompt("Enter list item 3");
Item3 += "\n";
return [Item1, Item2, Item3];
}
var List = GetArray();
for (var i = 0; i < List.length; i++) {
console.log(List[i]);
//alert(List[i]);
}
</script>
First of all, function creates variables scope. So var List inside GetArray function will create variable List inside this function scope. I recommend return list in this case. Or create var List out of GetArray scope, and assign inside it.
Second note, return should be used in function. Inside loop it doesn't make sense in this case...
Comments
There are two problems with your code. The first one you're trying to access the variable List outside its scope. If you want to access it you have to define it outside the function GetArray(). The second problem is you have a return statement inside your for loop, it means your for loop will run only once.
Solution:
<script type="text/javascript">
var List;
function GetArray() {
var Item1 = prompt("Enter list item 1");
Item1 += "\n";
var Item2 = prompt("Enter list item 2");
Item2 += "\n";
var Item3 = prompt("Enter list item 3");
Item3 += "\n";
List = new Array(Item1, Item2, Item3);
}
GetArray();
for (i = 0; i < List.length; i++) {
console.log(List[i]);
}
</script>
Comments
Thanks everyone. I've changed "return List[i]" to "document.write (List[i]);", as it will print it to the screen.
Here's what I've now got (as a result of answers here), and works fine:
<script type="text/javascript">
function GetArray() {
var Item1 = prompt("Enter list item 1");
Item1 += "\n";
var Item2 = prompt("Enter list item 2");
Item2 += "\n";
var Item3 = prompt("Enter list item 3");
Item3 += "\n";
List = new Array(Item1, Item2, Item3);
}
GetArray();
for (i = 0; i < List.length; i++) {
document.write (List[i]);
}
</script>
That's the problem with trying to learn it from a book. It's not responding when I try to ask it questions where it hasn't explained it properly.