Wormdog1
Wormdog1

Reputation: 1

'undefined' result for variable in Javascript function

something in my code is meaning that I am getting an array printed which contains only undefined values, not the random numbers I am expecting.

My code is below:

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();

The code is supposed to print a random list of number between the min and max values. I'm getting an array with 15 values in, but they are all undefined. I'm guessing this mean there is something wrong with my first 'for' loop?

If anyone has any suggestions on how I could make this better please do criticise!

Thanks in advance.

Upvotes: 0

Views: 99

Answers (6)

Charles Guo
Charles Guo

Reputation: 170

I think you swap the position of max and min when you initiate newList.

Change the line to:

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

Then it should work fine.

Upvotes: 1

Eren Akkus
Eren Akkus

Reputation: 471

Just replace this line;

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

with;

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

Upvotes: 0

Alex Gonz&#225;lez
Alex Gonz&#225;lez

Reputation: 31

I think max and min parameter are swapped

This

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

should be

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

Upvotes: 0

Lew
Lew

Reputation: 1278

I think maybe you max and min arguments were in the wrong order. You were trying to access the fullArray with a negative index due to subtracting a larger number from a smaller one.

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();

Upvotes: 0

Joseph Marikle
Joseph Marikle

Reputation: 78590

You have min and max mixed up in your arguments list. this results in an impossible boundary for your numbers (greater than 100 but less than 12). Just swap the parameters in the first line from max,min to 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();

Upvotes: 1

Weedoze
Weedoze

Reputation: 13953

You were pushing fullArray[randomItem] that contains nothing. It is never initialized

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();

Upvotes: 0

Related Questions