From b2ff198962546ed2851dab623d7deb6651d0e41d Mon Sep 17 00:00:00 2001 From: Ali Elsheikh <47045107+AliEl4eikh@users.noreply.github.com> Date: Thu, 24 Mar 2022 13:54:52 +0200 Subject: [PATCH] [Chapter 5] Update the matrix function for jagged Arrays We may loop for the number of rows and create an array with the number of columns, not the opposite. for (var i=0; i < columns; i +=1) { jaggedarray[i]=new Array(rows); } This loop will work as the columns and rows are equal, But if we want to create a jagged Array with 2 Rows and 3 Columns, this will not give us the expected result --- Chapter5(Arrays).js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Chapter5(Arrays).js b/Chapter5(Arrays).js index 2decd62..54ca3d2 100644 --- a/Chapter5(Arrays).js +++ b/Chapter5(Arrays).js @@ -290,8 +290,8 @@ console.log(sum); // prints 11 function Matrix(rows, columns) { var jaggedarray = new Array(rows); - for (var i=0; i < columns; i +=1) { - jaggedarray[i]=new Array(rows); + for (var i=0; i < rows; i +=1) { + jaggedarray[i]=new Array(columns); } return jaggedarray; }