I have some simple code that I've written in Python. It's working. Now I'm trying to port it to JavaScript. The code is here. I'm getting this error:
sketch.js:7 Uncaught TypeError: Cannot read properties of undefined (reading 'compression_ratio')
at w (sketch.js:7:39)
at ifsp (sketch.js:37:13)
at draw (sketch.js:54:14)
at p5._main.default.redraw (p5.js:71990:27)
at _draw (p5.js:64140:25)
What's going wrong here?
CodePudding user response:
The error is here (Starting line 28).
if (prob_sum > r) {
attractor_index = j - 1;
} else {
attractor_index = j;
}
If r is smaller than 1/3 (probability value that you have defined in line 51) you will decrement attractors_index using j - 1 and if that happens in the first iteration attractors_index will be -1 as j = 0. In that case attractors[attractors_index] will return undefined and you call w() with undefined instead of the object you expect, hence attractors.compression_ratio will not have a property compression_ratio and the error is thrown.
You can check/ reproduce that easily by manually setting r to > or < 1/3.
