Error forEach in Tetris. How to fix?

Good day to all.

Decided to learn JS by writing Tetris. The skeleton found on the Internet and began to finish the rest myself.

The problem is that all the pieces except the red, pressing the up key (to turn the figure) begin to break the web application. Don't you know what it is, but here is what the console:
Uncaught TypeError: Cannot read property 'forEach' of undefined
 at drawMatrix (index.html:32)
 at draw (index.html:28)
 at update (index.html:117)


And here is an excerpt of that code where the error happens:
function drawMatrix(matrix, offset) {
 matrix.forEach((row, y) => { // ERROR!
 row.forEach((value, x) => {
 if (value !== 0) {
 if (type === 1) {
 context.fillStyle = "green";
 context.fillRect(x + offset.x, y + offset.y, 1, 1);
 } else if (type === 2) {
 context.fillStyle = "blue";
 context.fillRect(x + offset.x, y + offset.y, 1, 1);
 } else if (type === 3) {
 context.fillStyle = "yellow";
 context.fillRect(x + offset.x, y + offset.y, 1, 1);
 } else if (type === 4) {
 context.fillStyle = "red";
 context.fillRect(x + offset.x, y + offset.y, 1, 1);
 } 
}
});
});
}


Well, all the code I decided to copy in the Online IDE. There's basically all the code is there, it is possible to run and test it.

P. S: I'm new to JS, so I hope for your understanding.
June 14th 19 at 19:13
1 answer
June 14th 19 at 19:15
Solution
You getMatrix returns a void(undefined), because there is the type 4, prescribe conditions for all types

function getMatrix(arg, type) {
 if (type === 4) {
....
}
 };
What I need to return something?) And I in the tank - helga.Jacobson53 commented on June 14th 19 at 19:18
Ahhh, I understand - helga.Jacobson53 commented on June 14th 19 at 19:21
Thank you very much! - helga.Jacobson53 commented on June 14th 19 at 19:24

Find more questions by tags HTMLJavaScriptWeb Development