Having some trouble with iterating through an array that is assigned to a variable in my code editor. Despite using
console.log(variable_name.length)
and successfully getting the array length, I keep getting an error saying the variable is undefined when trying to iterate through it.
function availableActions(state, reward_matrix) {
var current_state_row = reward_matrix[state];
console.log(current_state_row.length);
let av_act = [];
for (var i = 0; i < current_state_row.length; i++) {
if (current_state_row[i] != 0) {
av_act.push(i);
}
}
return av_act;
}
When running the script, I'm encountering this error:
TypeError: current_state_row is Undefined
This code snippet returns undefined
with a series of arrays, possibly causing the issue.
Minimal Reproducible Example (MRE)
function getRand(min, max) {
let rand = Math.random();
let number = rand * (max - min) + min;
number = Math.round(number);
return number;
}
let total = 0;
let grid = new Array(100)
for(var i = 0; i <100; i++){
grid[i] = []
for(var j = 0; j < 10; j++){
grid[i].push(0)
total++
}
}
for (var i = 0; i < 200; i++) {
let x = getRand(0, grid.length);
console.log(grid[x]);
}