0

I'm trying to generate an array of random digits, but I'm getting "undefined" at the beginning of each row. I've been searching online for a couple of hours, but haven't been able to figure it out.

The expected output should be 5 rows of 2 random digits like this:

87
57
81
80
02

but the actual output looks like this:

undefined87
undefined57
undefined81
undefined80
undefined02

This is a modified excerpt that produces the result shown above:

function NumberSet() {

  // generate all the digits
  this.generate = function() {
    random_digits = [];

    // create 5 rows of 2 random digits
    for(i=0; i<5; i++) {
      for(z=0; z<2; z++) {
        // use .toString() in order to concatenate digits to 
        // the array without adding them together
        random_digit = Math.floor(Math.random()*10).toString();
        random_digits[i] +=random_digit;
      }
    }
    return random_digits;
  }
}
randomnumbers1 = new NumberSet();
mynums = randomnumbers1.generate();
jQuery.each(mynums, function(i, l) {
  // display output in a div#nums
  $('#nums').append(l + '<br>');
});

The final version won't be using this method to display the digits. I'm just trying to troubleshoot where the "undefined" is coming from.

4 Answers 4

2

Initialize your variables

 random_digits[i] = "";
 for(z=0; z<2; z++) {
    random_digit = Math.floor(Math.random()*10).toString();
    random_digits[i] +=random_digit;
  }

Declare the variables properly with var.

var random_digit, random_digits = [];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This was what I was missing: random_digits[i] = ""; I added your other tip as well.
1

Declare random_digit in the first for loop and assign an empty string. Go through the inner for loop appending your random numbers, and then push() to the array back in the outer for loop.

function NumberSet() {
  // generate all the digits -a meme should be attached here-
  this.generate = function() {
    random_digits = [];

    // create 5 rows of 2 random digits
    for(i=0; i<5; i++) {
      var random_digit = ""; //Declare it out here
      for(z=0; z<2; z++) {
        // use .toString() in order to concatenate digits to 
        // the array without adding them together
        random_digit += Math.floor(Math.random()*10).toString(); //Append it here
      }
      random_digits.push(random_digit); //Push it back here
    }
    return random_digits;
  }
}

Fiddle-dee-dee

OR Forget the inner loop and use recursion

function NumberSet() {

    // generate all the digits
    this.generate = function () {
        random_digits = [];

        // create 5 rows of 2 random digits

        // Use i for how many numbers you want returned!
        var random_digit = function (i) {
            var getRand = function() {
                return (Math.floor(Math.random() * 10).toString());
            }
            return (i > 0) ? getRand()+random_digit(i-1) : "";
        };

        for (i = 0; i < 5; i++) {
            random_digits.push(random_digit(2)); //In this case, you want 2 numbers
        }
        return random_digits;
    }
}

Fiddle-do-do

And the final version because I'm bored

function NumberSet(elNum, numLen) {
    this.random_digits = []; //Random digits array
    this.elNum = elNum; //Number of elements to add to the array
    this.numLen = numLen; //Length of each element in the array
    // generate all the digits

    this.generate = function () {
        // create 5 rows of 2 random digits
        var random_digit = function (i) {
            var getRand = function () {
                return (Math.floor(Math.random() * 10).toString());
            }
            return (i > 0) ? getRand() + random_digit(i - 1) : "";
        };
        for (i = 0; i < this.elNum; i++) {
            this.random_digits.push(random_digit(this.numLen)); 
        }
        return this.random_digits;
    }
}

randomnumbers1 = new NumberSet(5, 2).generate();

jQuery.each(randomnumbers1, function (i, l) {
    // display output in a div#nums
    $('#nums').append(l + '<br>');
});

Fiddle on the roof

1 Comment

Thanks for taking the time to write that out. I used the fix in the first comment that was at the top of the page, but learned a lot from reading your examples. (I'm still learning JS.)
0

Replace

random_digits[i] +=random_digit;

With

random_digits[i] = (random_digits[i]  == undefined ? '' : random_digits[i]) + random_digit;

Demo: Fiddle

1 Comment

The first digit will be lost if it is 0 since it is falsy. You need an explicit check for undefined.
0

Your function can be simplified to:

function NumberSet() {

  this.generate = function() {
    var random_digits = new Array();

    for (i = 0; i < 5; i++) {
        randnum = Math.floor(Math.random() * 99);
        random_digits[i] = (randnum < 10 ? '0' : 0) + randnum;
    }
    return random_digits;
  }
}

Live Demo

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.