2016-10-26 54 views
0

我的代碼中的某些內容意味着我得到的數組只包含未定義的值,而不是我期望的隨機數。'未定義'在Javascript函數中變量的結果

我的代碼如下:

function List(max, min, numLists, numItems) { 
 
    this.max = max, 
 
    this.min = min, 
 
    this.numLists = numLists, 
 
    this.numItems = numItems, 
 
    this.generateLists = function() { 
 
     var fullArray = []; 
 
     var completeArray = []; 
 

 
     for (i = this.min; i < this.max; i++) { 
 
      fullArray.push(i); 
 
     } 
 
     for (i = 0; i < numItems; i++) { 
 
      var randomItem = Math.floor(Math.random() * (1 + (this.max - this.min))); 
 
      completeArray.push(fullArray[randomItem]); 
 
     } 
 

 
     console.log(completeArray); 
 
    } 
 
} 
 

 
var newList = new List(12, 100, 1, 15); 
 

 
newList.generateLists();

的代碼應該打印的最小值和最大值之間的數隨機列出。我得到了一個有15個值的數組,但它們都是未定義的。我猜這意味着我的第一個'for'循環有問題嗎?

如果任何人有任何建議,我可以做得更好,請批評!

在此先感謝。

回答

0

您正在推動fullArray[randomItem],其中不包含任何內容。這是從來沒有初始化

function List(max, min, numLists, numItems) { 
 
    this.max = max, 
 
    this.min = min, 
 
    this.numLists = numLists, 
 
    this.numItems = numItems, 
 
    this.generateLists = function() { 
 

 

 
     var completeArray = []; 
 

 
     for (i = 0; i < numItems; i++) { 
 
     var randomItem = Math.floor(Math.random() * (1 + (this.max - this.min))); 
 
     completeArray.push(randomItem); 
 
     } 
 

 
     document.write(completeArray); 
 

 
    } 
 
} 
 

 
var newList = new List(12, 100, 1, 15); 
 

 
newList.generateLists();

1

你有minmax您的參數列表混合起來。這會導致您的數字不可能出現邊界(大於100但小於12)。只需將第一行中的參數從max,min更改爲min,max即可。

function List(min,max,numLists,numItems){ 
 
    this.max = max, 
 
    this.min = min, 
 
    this.numLists = numLists, 
 
    this.numItems = numItems, 
 
    this.generateLists = function(){ 
 
     
 
     
 
     var fullArray = []; 
 
     var completeArray = []; 
 
     
 
     for (i = this.min ; i<this.max ; i++) { 
 
      fullArray.push(i); 
 
      } 
 
     
 
     for (i = 0 ; i<numItems ; i++) { 
 
      var randomItem = Math.floor(Math.random() * (1+(this.max-this.min))); 
 
      completeArray.push(fullArray[randomItem]); 
 
      } 
 
      
 
     console.log(completeArray); 
 
     
 
     } 
 
} 
 
     
 
var newList = new List (12 , 100 , 1,15); 
 

 
newList.generateLists();

+0

這就是它,doh!謝謝 – Wormdog1

+0

@ Wormdog1不客氣。很高興我能幫上忙。 –

0

我想也許你最大值和最小值論據是錯誤的順序。您嘗試訪問fullArray負指數,因爲從較小的數字中減去較大的數字。

function List(min,max,numLists,numItems){ 
 
    this.max = max, 
 
    this.min = min, 
 
    this.numLists = numLists, 
 
    this.numItems = numItems, 
 
    this.generateLists = function(){ 
 
     
 
     
 
     var fullArray = []; 
 
     var completeArray = []; 
 
     
 
     for (i = this.min ; i<this.max ; i++) { 
 
      fullArray.push(i); 
 
      } 
 
     for (i = 0 ; i<numItems ; i++) { 
 
      var randomItem = Math.floor(Math.random() * (1+(this.max-this.min))); 
 
      console.log(randomItem) 
 
      completeArray.push(fullArray[randomItem]); 
 
      } 
 
      
 
     console.log(completeArray); 
 
     
 
     } 
 
} 
 
     
 
var newList = new List (12 , 100 , 1,15); 
 

 
newList.generateLists();

0

我認爲最大和最小參數被交換

function List(max,min,numLists,numItems){ 

應該

function List(min,max,numLists,numItems){ 
0

當您啓動newList時,我認爲您會調換最大值和最小值的位置。

行更改爲:

var newList = new List (100, 12 , 1,15); 

那麼它應該工作的罰款。

0

只需更換此行;

var randomItem = Math.floor(Math.random() * (1+(this.max-this.min))); 

with;

var randomItem = Math.floor(Math.random()*(fullArray.length)+1);