Edward Hall
Edward Hall

Reputation: 21

JS 2D Array Error: "TypeError: Cannot read property '0' of undefined"

Working on a project for a class and keep getting this error when I want to reference an location in my 2D array.The project is a video game recommender system using pearson correlations. To get the correlations we are pulling results from a database filled usernames, the video game they ranked, and the rank(rating) they gave it. This is then used to create a userArray(array of all users who have ratings) and a rankArray(2D array pictured below with all games and the ratings users have given it) within the generateArrays function.

enter image description here

The error is coming from the line within my sim_pearson function which calculates the actual correlation value. When I try to reference a rating given by a particular user for a particular game, the error arise. I am unsure why as I have referenced the 2D array in a similar way before and had no issue.

Here is my sim_pearson function where the error occurs.

function sim_pearson(rankArray, person1, person2){
  var similaritems = [];
  for(var i = 0; i <= 9; i++){
    if (rankArray[i][person1] !== 0){
      if(rankArray[i][person2] !== 0){
        similaritems.push(i);
      }
    }
  }
  if (similaritems.length === 0){
    return 0;
  }
  console.log(similaritems);
  var n = similaritems.length;
  var temp1, temp2, sum1, sum2, sum1Sq, sum2Sq, pSum = 0;
  for (var x = 0; x <= n; x++){
    var sItem = similaritems[x];
    temp1 = rankArray[sItem][person1];
    temp2 = rankArray[sItem][person2];
    sum1 = sum1 + temp1;
    sum2 = sum2 + temp2;
    sum1Sq = sum1Sq + Math.pow(temp1, 2);
    sum2Sq = sum2Sq + Math.pow(temp2, 2);
    //sum of products
    pSum = pSum + (temp1 * temp2);
  }
  var num = pSum - (sum1*sum2/n);
  var den = Math.sqrt((sum1Sq-Math.pow(sum1, 2)/n)*(sum2Sq-Math.pow(sum2, 2)/n));
  if (den === 0){
    return 0;
  }
  var r = num/den;
  return r;
}

Here is where I create the arrays:

var userArray = [];
var rankArray = [];
function generateArray(result){
  //generate user array
  var i,item;
  var temp = 0;
  for (i in result.rows) {
    item = result.rows[i];
    var userExists = false;
    for (var x = 0; x<=userArray.length; x++){
      if(item.username == userArray[x]){
        userExists = true;
      }
    }
    if(userExists === false){
      userArray.push(item.username);
    }
  }
  for(var y =0; y<10; y++){
    rankArray[y] = [];
    for(var z = 0; z<userArray.length; z++){
      rankArray[y][z] = 0;
    }
  }
  //creating rankarray
  var w, item1;
  for(w in result.rows) {
    item1 = result.rows[w];
    for(var k in userArray) {
      if (item1.username == userArray[k]) {
        temp = k;
      }
    }
    rankArray[(item1.vgnum - 1)][temp] = item1.rank;
  }
}

And here is a picture of our arrays when they are created. userArray is first holding all the users, then rankArray which has an array set up in each index for each game(there are only 10) and within that array are the rankings of all the users(0 is user 1, 1 is user 2, etc). After is the similaritems array which just finds the games that two users have both rated.

enter image description here

The line that I am calling sim_pearson from essentially looks like this:

generateArrays(result);
//comparing the first two users
console.log("Pearson value: "+sim_pearson(rankArray, 0, 1);

Really unsure why this error is occuring and why I cannot set a var equal to a location with the rankArray I made like:

temp1 = rankArray[sItem][person1];

Any help would be fantastic!

Upvotes: 2

Views: 1649

Answers (1)

Barmar
Barmar

Reputation: 780724

This line is probably the problem:

for (var x = 0; x <= n; x++){

It should be x < n. Since n = similaritems.length, the indexes of similaritems run from 0 to n-1. So on the last iteration, when you do:

var sItem = similaritems[x];

it sets sItem = undefined. Then you do:

temp1 = rankArray[sItem][person1];

rankArray[undefined] is undefined, so this tries to access undefined[person1], which is not valid.

Upvotes: 1

Related Questions