0

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.

6 Answers 6

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

This was it, doh! Thanks
1

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.

Comments

0

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

Comments

0

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

Comments

0

I think max and min parameter are swapped

This

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

should be

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.