使用for循环循环函数的问题

时间:2016-10-28 10:49:47

标签: javascript

我对for循环有一些问题。当我运行代码时,该函数似乎运行一次,因为显示随机列表,但不是指定的预期数字。

任何人都可以帮助我吗?



function List(max,min,numLists,numItems){
    this.max = max,
    this.min = min,
    this.numLists = numLists,
    this.numItems = numItems,
    this.generateList = function (){
       
        var fullArray = [];
        var completeArray = [];
        var numItems = this.numItems
        
        
        //create an array of integers between min and max values
        for ( i = this.min ; i<(this.max+1) ; i++) {
            fullArray.push(i);
            }
            
        //select a random value from array of integers and add to new array    
        for ( j = 0 ; j<numItems ; j++) { 
            var randomItem = Math.floor(Math.random() * (fullArray.length));
            completeArray.push(fullArray[randomItem]);
            }
            
        //write new random list
        document.write(completeArray);
        
        }

    this.generateMultipleLists = function() {

    	var numLists = this.numLists;

    	//loop list creation to create multiple list arrays
    	for ( i=0 ; i<numLists ; i++ ){
    	  this.generateList();
    	}
    } 
}   
var newList = new List ( 100 , 12 , 7,15);
newList.generateMultipleLists();

   
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

不要创建全局变量 在函数generateMultipleLists中,在for循环中使用var i而不是i