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

Issue with looping a function using a for loop

本文关键字:循环 问题 函数 使用 for      更新时间:2023-09-26

我有一个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();
   

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