2013-04-28 65 views
1

我想将包含随机数(0到10)的数组插入一个更大的数组中,一旦其内容的总数将大约超过30个。但是输出会混乱。推动多维数组中的数组

var bigarray = new Array(); 
var smallarray = new Array(); 

var randNum = 0; 
var total = 0; 

for (var i = 0; i<10; i++){ 

    randNum = (10*Math.random()).toFixed(0); 
    total = total + randNum; 


    if(total>30) { 

     bigarray.push(smallarray) 

     smallarray.length=0; 
     smallarray.push(randNum); 
     total = randNum; 

    } else { 

     smallarray.push(randNum); 

    } 

} 

alert(" BIG ARRAY IS "+bigarray); 
+0

当你把randNum修改成一个字符串... – kennebec 2013-04-28 18:32:21

+0

http://stackoverflow.com/about – Xotic750 2013-05-11 10:12:37

回答

0

我对您的代码进行了更改,并提出了此问题。

var bigarray = []; 
var smallarray = []; 

var randNum = 0; 
var total = 0; 

for (var i = 0; i < 10; i += 1) { 
    randNum = Math.floor(10 * Math.random()); // you will never have a value of 10? 
    total = total + randNum; 

    if (total > 30) { 
     bigarray.push(smallarray.slice()) 
     smallarray.length = 0; 
     smallarray.push(randNum); 
     total = randNum; 
    } else { 
     smallarray.push(randNum); 
    } 
} 

alert(" BIG ARRAY IS " + bigarray); 

jsfiddle

事情我改变了:

通过beautifier

改变了你的new Array用途[]

{}和冉代码[]

使用{}而不是新的Object()。使用[]而不是新的Array()。

由于对象和阵列可以由用户

更改++被覆盖到+= 1

这种图案可以是混乱。

退房Code Conventions for the JavaScript Programming Languagejslint

新增array.slice当你推到smallarraybigarray,这使得在这种情况下副本。了解javascript的工作原理非常重要,请阅读Is JavaScript a pass-by-reference or pass-by-value language?如果不使用使数据复制为只包含基本数据的片段,则当您将数组长度设置为0时,数据就会丢失。

改变了你的number.toFixed用途Math.floor使得randNum存在一些

注:Math.random范围返回一个浮点,伪随机数[0,1],是从0(含)最多但不包括1(独家)

无论您的代码现在生成您的预期结果,我无法确定您的描述,但这应该是一个很好的起点。

+0

这太棒了!万分感谢。那么,当你使用push而不是数组切片时会发生什么?当涉及到数组时,'push'的行为是否有所不同? – 2013-04-29 02:23:06

+0

我用链接更新了答案和一些进一步的解释,以便为您澄清情况。 – Xotic750 2013-04-29 06:16:41

0

两种错误的做法事情是在第一眼看到代码

(1)的代替

randNum = (10*Math.random()).toFixed(0); 

你可能想

randNum = Math.floor(11*Math.random()); 
  • 数学。地板代替toFixed() - 见成数字返回0〜10 @kennebec评论的
  • 11而不是10,如0 <= Math.random() < 1

(2)以下行推(多次)的参考相同的小物体。

bigarray.push(smallarray); 

在接下来的步骤中,您使用smallarray.length = 0清除数组。由于该数组未被复制到bigarray中,但仅被引用,所生成的项目将丢失。

编辑:我看了你的问题错了 - 答案的其余部分是固定的

你可能想推smallarray到bigarray的副本,所以用以下内容替换上述行:

bigarray.push(smallarray.slice(0)); 
+0

像一个魅力工作。万分感谢。 – 2013-04-29 02:27:26

0
var bigarray = new Array(); 
    var smallarray = new Array(); 
    var randNum = 0; 
    var total = 0; 
    for (var i = 0; i < 10; i++) { 
     for (var j = 0; j < smallarray.length; j++) { 
      total = total + smallarray[j]; 
     } 
     if (total <= 30) 
     { 
      randNum = Math.floor((Math.random() * 10) + 1); 
      smallarray.push(randNum); 
     } 
     else { 
      bigarray.push(smallarray.slice(0));     
      smallarray.length = 0; 
     } 
     total = 0;  

    } 
    alert(" BIG ARRAY IS " + bigarray); 
0

您需要的最主要的一个内部另一个循环来填充smallarray,是这样的:

var bigarray = new Array(); 

for (var i = 0; i<10; i++){ 

    // moving the variable declarations inside this loop means they are re-set for each small array 
    var smallarray = new Array(); 
    // create the first entry for the small array 
    var randNum = Math.floor(11*Math.random()); 
    var total = randNum; 

    // loop to populate the small array 
    while(total <= 30){ 
     smallarray.push(randNum); 
     randNum = Math.floor(11*Math.random()); 
     total += randNum; 
    } 
    bigarray.push(smallarray) 
}