2016-10-26 57 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);