Trevor
Trevor

Reputation: 2457

selecting values from javascript in a for loop

I made this little function yesterday and it was working fine. This morning I check it and it's not working anymore. I have no idea why.

The code gives me an error at the first call of 'var color = dataPHP[i]['color_code'];'

TypeError: 'undefined' is not an object (evaluating 'dataPHP[i]['color_code']') global code

  var color_code_hold = [];
  for (ix = 0; ix <= 5; ++ix){
    for (i = 0; dataPHP.length; ++i) {
      var color = dataPHP[i]['color_code'];
      if(color === ix){
        // alert('found ix=='+ix+'color==='+color);
        color_code_hold[color_code_hold.length] = color;
        break;
      }
      if(dataPHP.length >= 20){
        var check_length = dataPHP.length-4;
      }else{
        var check_length = dataPHP.length;
      }
      if(i >= check_length){
        // omit ix
        // alert('broke'+ix);
        break;
      }
    }
  }

  var total_value = 0;
  for(i = 0; i < color_code_hold.length; ++i){
    var cur_color_h = color_code_hold[i];// held value
    // alert('cur_color_h val'+cur_color_h);
    if(cur_color_h === 0){
      total_value = total_value+25;
      // alert('added 0');
    }else
    if(cur_color_h === 1){
      total_value = total_value+25;
      // alert('added 1');
    }else 
    if(cur_color_h === 2){
      total_value = total_value+20;
      // alert('added 2');
    }else
    if(cur_color_h === 3){
      total_value = total_value+15;
      // alert('added 3');
    }else
    if(cur_color_h === 4){
      total_value = total_value+10;
      // alert('added 4');
    }else
    if(cur_color_h === 5){
      total_value = total_value+5;
      // alert('added 5');
    }
  }

  math_rand_num = Math.floor((Math.random() * total_value) + 1);

  color_code_hold.sort(function(a, b){return a-b});

  first_add_value = 0;
  for(i = 0; i < color_code_hold.length; ++i){
    var cur_color = color_code_hold[i];
    if(cur_color === 0){
      second_add_value = first_add_value + 25;
      if(math_rand_num <= second_add_value){
        var select_color = 0;
        break;
      }else{
        first_add_value = second_add_value;
      }
    }
    if(cur_color === 1){
      second_add_value = first_add_value + 25;
      if(math_rand_num <= second_add_value){
        var select_color = 1;
        break;
      }else{
        first_add_value = second_add_value;
      }
    }
    if(cur_color === 2){
      second_add_value = first_add_value + 20;
      if(math_rand_num <= second_add_value){
        var select_color = 2;
        break;
      }else{
        first_add_value = second_add_value;
      }
    }
    if(cur_color === 3){
      second_add_value = first_add_value + 15;
      if(math_rand_num <= second_add_value){
        var select_color = 3;
        break;
      }else{
        first_add_value = second_add_value;
      }
    }
    if(cur_color === 4){
      second_add_value = first_add_value + 10;
      if(math_rand_num <= second_add_value){
        var select_color = 4;
        break;
      }else{
        first_add_value = second_add_value;
      }
    }
    if(cur_color === 5){
      second_add_value = first_add_value + 5;
      if(math_rand_num <= second_add_value){
        var select_color = 5;
        break;
      }else{
        first_add_value = second_add_value;
      }
    }
  }

  for (i = 0; i < dataPHP.length; ++i) {
    var color = dataPHP[i]['color_code'];
    if(color === select_color){
      var key = i;
      break;
    }
  }

I typed in this little bit of code to check it, and it stilled errored on the previously stated line.

var i = 0;
var color = dataPHP[i]['color_code'];
alert(color);

I really have no idea why it's not working. Can you not put certain variables in for loops? How could it work yesterday and mysteriously break today?

Also, here's a small sample of the array.

var dataPHP = [
{"deck_id":"1001","card_key":"1005","front_side":"s1","back_side":"s2","card_three":"s3","last_seen":0,"color_code":0,"last_study":0,"placement":1},
{"deck_id":"1001","card_key":"1004","front_side":"a2","back_side":"a1","card_three":"a3","last_seen":0,"color_code":5,"last_study":1414769400,"placement":2}];

Upvotes: 0

Views: 38

Answers (1)

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

Assuming that dataPHP is defined first. I don't see a problem with your test case. Although notice your for loop has a problem in the condition:

for (i = 0;  dataPHP.length; ++i) {

Needs to be:

for (i = 0; i < dataPHP.length; ++i) {

i must of gotten larger than dataPHP.length causing the infinite loop to error out.

Here is an example

Upvotes: 2

Related Questions