通过可变数量的数组循环变量次数? [英] Looping through variable number of arrays variable times?

查看:64
本文介绍了通过可变数量的数组循环变量次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将传递我的函数一个不同长度的二维数组。

在该数组中是一个数据点,它应循环的次数

通过。


所以,例如,我可能会将此传递给函数:


example [0] = new Array(" A" ,2);

示例[1] =新数组(" Q",4);

函数loopIt(示例);


该函数将执行以下操作:


函数loopIt(示例){

for(x1 = 0; x1< example [0 ] [1]; x1 ++){

for(x2 = 0; x2< example [1] [1]; x2 ++){

calculateArray = new Array(示例.length);

calculateArray [0] =新数组(例[0] [0],x1);

calculateArray [1] =新数组(例子[1] ] [0],x2);

计算(calculateArray);

}

}

}


但是loopIt()不知道example.length会是什么。在这里我展示了

example.length = 2.但是如果exam​​ple.length = 41,这个函数就不会工作了。当example.length会有所不同时,如何创建这样的循环?


谢谢!


Mike

解决方案



Mike P写道:

example [0] = new Array(" A",2);
example [1] = new Array(" Q",4);
function loopIt(example);

该函数将执行以下操作:

function loopIt(example){
for(x1 = 0; x1< example [0] [1]; x1 ++){
for(x2 = 0; x2< example [1] [1]; x2 ++){
calculateArray = new Array(example.length);
calculateArray [0] = new Array(example [0] [0],x1);
calculateArray [ 1] =新数组(例[1] [0],x2);
计算(calculateArray);
}
}
}

但是loopIt()不知道example.length会是什么。在这里我展示了
example.length = 2.但是如果exam​​ple.length = 41,这个函数就不会起作用了。当example.length会发生变化时,如何创建这样的循环?




这意味着你的数组元素是成对出现的:example [i] [1]

为您提供外部循环的值,示例[i + 1] [1]为您提供内部值的

值。因此,您的数组无法包含

奇数元素。这是正确的吗?




" VK" < SC ********** @ yahoo.com>在消息中写道

新闻:11 ********************** @ o13g2000cwo.googlegr oups.com ...

这意味着您的数组元素是成对出现的:example [i] [1]
为您提供外部循环的值,示例[i + 1] [1]为您提供
内部价值。因此,您的数组无法包含奇数个元素。这是正确的吗?




使用代码示例,示例数组可以包含1到41个元素(甚至是

或者奇数),但是其中的每个数组都是2长。


所以:

example.length = 0到41个元素,以及中间的任何整数(偶数或奇数)

但是示例[x] .length将始终= 2其中

示例[x] [0]将始终包含一个字符串并且

示例[x] [1]将包含0到8之间的整数


如果我是为了编写这个漫长而丑陋的方式,我可以创建一个基于

example.length的开关,如下所示:


函数loopIt(示例){


开关(example.length){

案例1:

for(x1 = 0; x1< example [0] [ 1]; x1 ++){

calculateArray = new Array(example.length);

calculateArray [0] = new Array(example [0] [0],x1) ;

计算(calculateArray);

}

}

休息;

案例2:

for(x1 = 0; x1< example [0] [1]; x1 ++){

for(x2 = 0; x2< example [ 1] [1]; x2 ++){

calculateArray = new Array(example.length);

calculateArray [0] = new Array(example [0] [0],x1);

calculateArray [1] = new Array(示例[1] [0],x2);

calculate(calculateArray);

}

}

}

休息;

案例3:

for(x1 = 0; x1<示例[0] [1]; x1 ++){

for(x2 = 0; x2< example [1] [1]; x2 ++){

for(x3 = 0; x3< example [ 2] [1]; x3 ++){

calculateArray = new Array(example.length);

calculateArray [0] = new Array(示例[0] [0] ,x1);

calculateArray [1] = new Array(示例[1] [0],x2);

calculateArray [2] = new Array(示例[2] ] [0],x3);

计算(calculateArray);

}

}

}

休息;

}

}


等等,直到案例41,但是这似乎是非常低效的。

我希望有更清洁的方法来实现这个目标。

你跟着吗?


我想有一些方法可以用递归数组或者动态创建函数(ala,new Function)来实现这个...我只是不太明白

如何去做。


感谢您对此的帮助!


Mike


>我想有一些方法可以使用递归数组或动态

创建函数(ala,new Function)......我只是不太明白
如何去吧。




好​​吧,使用运行时生成的函数:


< html>

< head>

< title>测试< / title>

< meta http-equiv =" Content-Type" content =" text / html;

charset = iso-8859-1">

< script type =" text / javascript">

var example = new Array();

example [0] = new Array(''Q'',2);

example [1] = new Array(''A'',4);

示例[2] =新数组('''',3);


function test(){

var fun = null;

var funOpen ='''';

var funBody ='''';

var funEnd ='''';

var x ='''';

var space ='''';

var len = example.length;


for(i = 0; i< len; i ++){

x =''x ''+(i + 1);

funOpen + = space +''for(''+ x +''= 0;''+ x +''< arr [''+ i +''] [1];''+ x +''++){\ n'';

funEnd + =''}'';

space + =''' ';

}


funBody + = space +''calculateArray = new Array(arr.length); \ n'';
for(i = 0; i< len; i ++){

x =''x''+(i + 1);

funBody + = space +''calculateArray [''+ i +''] = new

数组(arr [''+ i +''] [0],''+ x +''); \ n' ';

}

funBody + = space +''calculate(calculateArray); \ n'';

fun = new Function('' arr'',funOpen.concat(funBody,funEnd));

fun(example);

}


函数计算(foo){

alert(foo);

}

< / script>

< / head>


< body bgcolor =" #FFFFFF" onload =" test()">


< / body>

< / html>


我不会说它比41个案子更难看,但绝对比b $ b更短。 :-)

我想任何数学教授都会因为上述事情而杀了我。

循环的总量是数组中[i] [1]值的阶乘。这是一个强烈的提示

,这个问题可以用一些漂亮而深刻的学术化方式来解决,使用递归方式。但它需要比任何JavaScript技能更好的矩阵数学知识。最后一个很好的我,第一个是

漂浮得很惨:-(

它有效吗......


I will be passing my function a two dimensional array of varying length.
Within that array is one data point, and the number of times it should loop
through.

So, for example, I might pass this to the function:

example[0] = new Array("A",2);
example[1] = new Array("Q",4);
function loopIt(example);

The function will then do the following:

function loopIt(example) {
for (x1 = 0; x1 < example[0][1]; x1++) {
for (x2 = 0; x2 < example[1][1]; x2++) {
calculateArray = new Array(example.length);
calculateArray[0] = new Array(example[0][0],x1);
calculateArray[1] = new Array(example[1][0],x2);
calculate(calculateArray);
}
}
}

But loopIt() doesn''t know what example.length will be. Here I showed
example.length = 2. But if example.length = 41, this function wouldn''t
work. How can I create a loop like this when example.length will vary?

Thanks!

Mike

解决方案


Mike P wrote:

example[0] = new Array("A",2);
example[1] = new Array("Q",4);
function loopIt(example);

The function will then do the following:

function loopIt(example) {
for (x1 = 0; x1 < example[0][1]; x1++) {
for (x2 = 0; x2 < example[1][1]; x2++) {
calculateArray = new Array(example.length);
calculateArray[0] = new Array(example[0][0],x1);
calculateArray[1] = new Array(example[1][0],x2);
calculate(calculateArray);
}
}
}

But loopIt() doesn''t know what example.length will be. Here I showed
example.length = 2. But if example.length = 41, this function wouldn''t
work. How can I create a loop like this when example.length will vary?



That means that your array elements are going by pairs: example[i][1]
gives you the value for external loop, example[i+1][1] gives you the
value for internal value. So there is no way your array may contain an
odd amount of elements. Is it correct?



"VK" <sc**********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

That means that your array elements are going by pairs: example[i][1]
gives you the value for external loop, example[i+1][1] gives you the
value for internal value. So there is no way your array may contain an
odd amount of elements. Is it correct?



With the code sample, the "example" array can contain 1 to 41 elements (even
or odd), but each array within it will be 2 long.

So:
example.length = 0 to 41 elements, and any integer inbetween (even or odd)
but example[x].length will always = 2 where
example[x][0] will always contain a string and
example[x][1] will contain an integer between 0 and 8

If I were to code this a long and ugly way, I could create a switch based on
example.length like this:

function loopIt(example) {

switch(example.length) {
case 1:
for (x1 = 0; x1 < example[0][1]; x1++) {
calculateArray = new Array(example.length);
calculateArray[0] = new Array(example[0][0],x1);
calculate(calculateArray);
}
}
break;
case 2:
for (x1 = 0; x1 < example[0][1]; x1++) {
for (x2 = 0; x2 < example[1][1]; x2++) {
calculateArray = new Array(example.length);
calculateArray[0] = new Array(example[0][0],x1);
calculateArray[1] = new Array(example[1][0],x2);
calculate(calculateArray);
}
}
}
break;
case 3:
for (x1 = 0; x1 < example[0][1]; x1++) {
for (x2 = 0; x2 < example[1][1]; x2++) {
for (x3 = 0; x3 < example[2][1]; x3++) {
calculateArray = new Array(example.length);
calculateArray[0] = new Array(example[0][0],x1);
calculateArray[1] = new Array(example[1][0],x2);
calculateArray[2] = new Array(example[2][0],x3);
calculate(calculateArray);
}
}
}
break;
}
}

So on, and so forth up to case 41, but that seems excessively inefficient.
I''m hoping there''s a cleaner way to accomplish this.
do you follow?

I imagine there''s some way to do this with a recursive array or dynamically
creating the function (ala, new Function)... I just don''t really understand
how to go about it.

Thanks for your help on this!

Mike


> I imagine there''s some way to do this with a recursive array or dynamically

creating the function (ala, new Function)... I just don''t really understand
how to go about it.



Well, using runtime generated function:

<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
var example = new Array();
example[0] = new Array(''Q'',2);
example[1] = new Array(''A'',4);
example[2] = new Array(''B'',3);

function test() {
var fun = null;
var funOpen = '''';
var funBody = '''';
var funEnd = '''';
var x = '''';
var space = '''';
var len = example.length;

for (i=0; i<len; i++) {
x = ''x''+(i+1);
funOpen+= space+''for (''+x+''=0; ''+x+''<arr[''+i+''][1]; ''+x+''++) {\n'';
funEnd += ''}'';
space+= '' '';
}

funBody+= space+''calculateArray = new Array(arr.length);\n'';
for (i=0; i<len; i++) {
x = ''x''+(i+1);
funBody+= space+''calculateArray[''+i+''] = new
Array(arr[''+i+''][0],''+x+'');\n'';
}
funBody+= space+''calculate(calculateArray);\n'';
fun = new Function(''arr'',funOpen.concat(funBody,funEnd));
fun(example);
}

function calculate(foo) {
alert(foo);
}
</script>
</head>

<body bgcolor="#FFFFFF" onload="test()">

</body>
</html>

I would not say that it''s any less ugly than 41 case, but definitely
shorter. :-)
I guess any math prof would kill me for the above. The total amount of
loops is factorial of [i][1] values in your array. It''s a strong hint
that the problem could be solved in some nice and profoundly academical
way using recursions. But it requires good matrix math knowledge more
than any JavaScript skills. The last of fine by me, the first is
floating miserably :-(
It works any way...


这篇关于通过可变数量的数组循环变量次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆