JaceG
JaceG

Reputation: 124

JavaScript coding error

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>

Upvotes: 0

Views: 74

Answers (5)

JaceG
JaceG

Reputation: 124

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.

Upvotes: 0

Sunny
Sunny

Reputation: 402

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.

Upvotes: 1

Golden_flash
Golden_flash

Reputation: 492

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

enter image description here

Upvotes: 2

Andr&#233;
Andr&#233;

Reputation: 156

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>

Upvotes: 1

Arūnas Smaliukas
Arūnas Smaliukas

Reputation: 3321

<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...

Upvotes: 1

Related Questions