Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to get this to work and I hope this isn't too vague. -Chris

var example = new Array();

var test1 = "one";
var test2 = "two";
var test3 = "three";

for (v = 1; v <= 3; v++) {
    alert(example[test + v])
}

I want the alert to say:

one
two
three

share|improve this question
do you want a single alert to say "one two three" or 3 alerts to say "one" then "two" then "three" ? – Matthew J Morrison Jun 11 '10 at 16:22
I'm not faulting you for not knowing the basics, we are all at different levels here, but I would suggest quickly going through the tutorial at w3schools.com/js and get some of the basics under your belt before asking questions. There is a section on there about arrays as well. – stephenbayer Jun 22 '11 at 15:50

1 Answer

up vote 1 down vote accepted

Your array example isn't initialized anywhere. It should be like this:

var example = new Array();
example[1] = "one";
example[2] = "two";
example[3] = "three";

for (v = 1; v <= 3; v++) {
    alert(example[v]);
}

This will make three separate alerts that say "one", then "two", then "three". If you want a single alert that says "one two three", you should do:

var example = new Array();
example[1] = "one";
example[2] = "two";
example[3] = "three";

var output = "";

for (v = 1; v <= 3; v++) {
    output += " " + example[v];
}

alert(output);

In response to Chris' request for follow-up:

In that case you could nest the loops, but it would be much more efficient to generate the output string once and then loop to display it. Example:

var output = "";
// This variable just makes it happen 3 times, it no longer corresponds to the array size
for (numAlerts = 1; numAlerts  <= 3; numAlerts ++) {

    // This is the actual index for the array
    for (v = 1; v <= example.length; v++) {
        output += " " + example[v];
    }
    alert(output);

    // Clean out the string or it will keep concatenating
    output = "";
}

That example has computational cost of n^2 (technically m x n), because you are nesting the loops. To make 3 alerts, each with the concatenation of 3 array elements, would take 9 significant operations. The next example:

var output = "";
// This variable is once more for array size
for (v = 1; v <= example.length; v++) {
    output += " " + example[v];
}

// This variable controls how many times the alert is printed
for (numAlerts = 1; numAlerts <= 3; numAlerts++) {    
    alert(output);
}

This one has computational cost 2n, reduced to n. In our example, 6 significant actions occur, 3 to concatenate the string, and 3 more to print it. 6 < 9, but not by much. However, imagine you have 300 items in the list, and want to print it 1000 times. Well, now 1300 << 300000.

One last note: in most languages, arrays are zero-based, so it would be example[0], [1], [2], etc. I understand you matched example[1] = "one" but it will make loops confusing if you start at 1. The standard for cycle for an array is:

var example = ["zero", "one", "two", "three"];
for (i = 0; i < example.length; i++) {
    action(example[i]);
}
share|improve this answer
Cool thanks Andy. I'll give that a try... – Chris Chiera Jun 11 '10 at 16:22
@Chris Chiera: You can initialize the array using an array-literal too. ["one", "two", "three"]; would give you the same array in a single line of code. – Andy E Jun 11 '10 at 16:23
Hey guys I have one more question. How would this work if I wanted the output to say: one, two, three one, two, three one, two, three one, two, three – Chris Chiera Jun 11 '10 at 16:41
Sorry, so it would be: line 1: one,two,three line2: one, two, three ect... – Chris Chiera Jun 11 '10 at 16:43
Andy: Thanks again for your help. This will give me something to study for the next week! Cheers! – Chris Chiera Jun 11 '10 at 17:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.