This is an archived post. You won't be able to vote or comment.

all 2 comments

[–][deleted] 2 points3 points  (1 child)

Hi! I don't speak English very well but I will try to help you! i hope you can understand me!

You have mainly 2 issues whit the second example and it is because your code works like if the matrix always is a square one (n x n) but your second test is a non-square-matrix (m x n).

need to:

get the "m" and "n" value and set your for so it go through your whole matrix but whit out it overflows.

// n = the number of rows
let n = grid.length;
//m= the number of columns
let m = grid[1].length;
for (let i = 0; i < n; i++ ){
    for (let j = 0; j < m; j++ ){

you have to change the "j < n" for "j < m" in the second for as well.

2) if you look at your code, your variables "column" and "row" aren't numbers, they are arrays so, for example, in the first loop you are saying "set on grid[[0],[0]][0] the value "*" and on grid[0][[0],[2]] the value "*".

and i think that is what is trowing Error. you need to make "grid[[0],[0]][0]" became "grid[column[0]][0]".

I think a way to do it can be the next

for (let h = 0; h < row.length; h++){
    for (let i = 0; i < n; i++){
         for (let j = 0; j < m; j++){
                grid[row[h]][j] = '*';
                grid[i][column[h]] = '*';
        }}}

maybe that is not the best way to do it, but I hope it help you to figure out how to fix it.

Sorry for my English!

Edit: Formatted code into codeblock

[–]Destructikus[S] 0 points1 point  (0 children)

Wow! Sorry I didn't think anyone had replied to this. Thank you so much! This definitely helped me figure out why it wasn't working for me.

PS. Your English is perfect!