Creating array in function

`function Perceptron(n,lines){
  this.weights = new Array(lines);
  console.log(this.weights);
  for (var i = 0; i < this.weights.length; i++){
    this.weights[i] = new Array(n);
  }
  this.lr = 0.002;
  for (var i = 0; i < this.weights.length; i++){
    for (var j = 0; j < this.weights[i].length; j++){
      this.weights[i][j] = random(-1,1);
    }
  }
  this.guesses = new Array(lines);
  for (var i = 0; i < this.guesses.length; i++){
    this.guesses[i] = function(inputs){
      sum = 0;
      console.log(n+','+lines);
      console.log(this.weights.length);
      for (var j = 0; j < this.weights[i].length; j++){
        sum += inputs[j]*this.weights[i][j];
      }
      return sign(sum);
    }
  }
}`

Here's my output: https://gyazo.com/318a9f2982f0a52efc735cfdb410caa2

As you can see, I first define 'this.weights' as a new array, with length of 'lines' and yes, both arguments to the function are numbers.

But for some reason, I can log this.weights, and it says it's undefined, and I guess that's because all of the elements are undefined?

But it throws an error at line 85, which is simply console.log(this.weights.length);

If I take that line out, then for (var j = 0; j < this.weights[i].length; j++){ throws an error, how can I fix this?

Answers

Sign In or Register to comment.