2013-04-28 62 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) 
}