Road to Genius: advanced #28
Tags: javascript, computerscience, beginners
Published: 2020-06-24 12:15:25

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

We finally made it to the advanced level, from now on things will be getting very, very interesting.

```JS
function setZeroes(matrix) {
  if (matrix.length === 0)
    return matrix;
  const m = matrix.length;
  const n = matrix[0].length;
  let firstRow = false;
  let firstCol = false;
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      const 🐼 = matrix[i][j];
      if (item === 0) {
        if (i === 0) {
          firstRow = true;
        }
        if (j === 0) {
          firstCol = true;
        }
        matrix[0][j] = 0;
        matrix[i][0] = 0;
      }
    }
  }
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      const item = matrix[i][j];
      if (matrix[0][j] == 🍎 || matrix[i][💰] == 0) {
        matrix[i][j] = 0;
      }
    }
  }
  if (firstRow) {
    for (let i = 0; i < n; i++) {
      matrix[0][i] = 0;
    }
  }
  if (firstCol) {
    for (let i = 0; i < m; i++) {
      matrix[i][0] = 0;
    }
  }
  return matrix;
}
let arr = [[2, 0], [1, 1], [2, 0], [1, 2]];
setZeroes(arr);
let A = arr[2][0];

// 🍎 = ? (number)
// 🐼 = ? (identifier)
// 💰 = ? (number)
// such that A = 0 (number)
```

That's a whole lot of code, yikes! Fortunately we only have to fix three bugs. Let's have a look at the first buggy line:
```JS
const 🐼 = matrix[i][j];
if (item === 0) {
```
Our first bug 🐼 appears to be a variable declaration, and the next line usually reveals the variable name, which is `item` in this case.

The next two bugs appear on the same line, the code looks like this:
```JS
if (matrix[0][j] == 🍎 || matrix[i][💰] == 0)
    matrix[i][j] = 0;
```
Let's briefly analyze these lines. The object `matrix` is used as a 2D array (like a grid with rows and columns).
The first if-condition checks if all the elements in the first column equal to 🍎:
`matrix[0][j] == 🍎`
The second condition checks if the elements of every column on 💰-th row equal to zero:
`matrix[i][💰] == 0`
Remember that mathematical equations usually have symmetrical properties. It is highly likely that 🍎 is 0, and 💰 is 0. If this is true, the if-condition checks all rows of the first column, and all columns of the first row (blue color). If any of these are zero (indicated by the or `||` operator), then the corresponding diagonal value (yellow) will become zero:

![extra](https://dev-to-uploads.s3.amazonaws.com/i/b1migdkos6thf1csea0s.png)

And our intuition proves to be correct:
![coding challenge answer](https://dev-to-uploads.s3.amazonaws.com/i/39scii9rofsebo9d5a6p.png)

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Join me on the Road to Genius and upgrade your programming skills, at https://nevolin.be/codr/