简体   繁体   English

Javascript循环/数组/变量问题

[英]Javascript Loop / Array / Variable question

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

Your array example isn't initialized anywhere. 您的数组example未在任何地方初始化。 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 mxn ), because you are nesting the loops. 该示例的计算成本为n ^ 2 (技术上为mxn ),因为您要嵌套循环。 To make 3 alerts, each with the concatenation of 3 array elements, would take 9 significant operations. 要发出3个警报(每个警报包含3个数组元素),将需要进行9次重要操作。 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 . 这个计算量为2n ,降为n In our example, 6 significant actions occur, 3 to concatenate the string, and 3 more to print it. 在我们的示例中,发生了6个重要操作,其中3个连接了字符串,另外3个用于打印字符串。 6 < 9, but not by much. 6 <9,但幅度不大。 However, imagine you have 300 items in the list, and want to print it 1000 times. 但是,假设您在列表中有300个项目,并且想打印1000次。 Well, now 1300 << 300000. 好吧,现在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: 最后一点:在大多数语言中,数组都是从零开始的,所以它是example[0], [1], [2], etc.我知道您匹配到example[1] = "one"但是它将循环如果你开始在1.标准混淆for一个数组周期是:

var example = ["zero", "one", "two", "three"];
for (i = 0; i < example.length; i++) {
    action(example[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM