3

I'm trying to get an "hourglass" array out from a nested array.

Say that this array is

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0

I have to get this "hourglass" array out and add it all up:

1 1 1
  1
1 1 1

Therefore, here's my attempt:

for (let i = 0; i < 3; i++) {
    for (let j = 0; j < 3; j++) {
        if ((i === 1 && j === 0) && (i === 1 && j === 2)) {
            continue;
        } else {
            newArr.push(arr[i][j]);
        }
    }
}

I noticed that if I use this condition: i === 1 && j === 0, it was able to skip over the position at i = 1 and j = 0 but when I added j = 2, I couldn't get it to skip over the first and third positions. Therefore, I was trying out various options to get it to work but I couldn't get it to work. Such, I was wondering if I'm doing the && operator correctly?

s0xzwasd
  • 2,895
  • 3
  • 17
  • 26
Kristina Bressler
  • 1,642
  • 1
  • 25
  • 60
  • 4
    Please click edit, then `[<>]` snippet editor and provide a [mcve] – mplungjan Dec 04 '20 at 11:55
  • 2
    This `if((i === 1 && j === 0) && (i === 1 && j === 2))` means j must be 0 AND 2 wich will never be true. I think you are looking for the OR operator `if((i === 1 && j === 0) || (i === 1 && j === 2))` – ptothep Dec 04 '20 at 11:56
  • get rid of zeros and print the array. – EugenSunic Dec 04 '20 at 12:01
  • 2
    please add the array, you are talking about. btw, you are comparing indices, not values. – Nina Scholz Dec 04 '20 at 12:01
  • guess it's a 3*6 matrix – KcH Dec 04 '20 at 12:09
  • I'd check the values, *not* the indexes - this way your code will work for any 'shape' of '1's. The answer below looks pretty good. – Gaz Dec 04 '20 at 12:14
  • Do you need only that particular shape in a 3x3 size? If yes, you can just hardcode the indices and maybe omit the loop completely - doesn't matter. If you need to support more sizes, please show us some more examples. – Bergi Dec 12 '20 at 19:40

2 Answers2

1

You could add spaces for the zeros (0) and for the ones (1) or maybe any other number just accumulate them;

const arr = [
  [1, 1, 1, 0, 0, 0],
  [0, 1, 0, 0, 0, 0],
  [1, 1, 1, 0, 0, 0],
]
let printRow = '';
for (let i = 0; i < arr.length; i++) {
  for (let j = 0; j < arr[i].length; j++) {
    if (arr[i][j] === 0) {
      printRow += ' ';
    } else {
      printRow += arr[i][j];
    }
  }
  console.log(printRow);
  printRow = '';
}
EugenSunic
  • 13,162
  • 13
  • 64
  • 86
  • I'm sorry for the confusion, is it related to the numbers itself in the array or to the positions? What if the array are different like this? `-9 -9 -9 1 1 1 2 -9 4 4 3 2 -9 -9 -9 1 2 3` That's why I need to look by positions/indexs. – Kristina Bressler Dec 06 '20 at 08:22
1

Solution

To find every hourglass in this grid 3 rows 6 columns grid with only one loop
Notice: When the Grid becomes larger you have to use an inner for loop

    const arr = [
        [1, 1, 1, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 0],
      ]  
       // format is [row][column]  
       let result;
       let topp;
       let mid;
       let bottom;
       for (let i = 0; i < 4; i++) {
      
               // this ist the filter defining what we are looking for
           if (arr[0][i] === 1 && arr[0][i+1] === 1 && arr[0][i + 2] === 1 &&
               arr[1][i] === 0 && arr[1][i+1] === 1 && arr[1][i + 2] === 0 &&
               arr[2][i] === 1 && arr[2][i+1] === 1 && arr[2][i + 2] === 1) {
               
              // here we build the string        
               topp = arr[0][i] +  " " + arr[0][i+1] + " " + arr[0][i+2];
               mid = arr[1][i] + " " + arr[1][i+1] + " " + arr[1][i+2];
               bottom = arr[2][i] + " " + arr[2][i+1] + " " + arr[2][i + 2];
       
               result = topp + "\n" + mid + "\n" + bottom;
               
           }
       }
       console.log(result)
Aalexander
  • 4,987
  • 3
  • 11
  • 34
  • put yourself into the perspective of the reader of that code – EugenSunic Dec 04 '20 at 12:17
  • Umm, the indizes correspond to the row column format. I dont think its so hard to understand especially with top mid bottom variable before calculating the sum... – Aalexander Dec 04 '20 at 12:32
  • do you need the difference between JS and Java? – EugenSunic Dec 04 '20 at 12:46
  • please tell me :) – Aalexander Dec 04 '20 at 12:59
  • Umm.... the keyword are JavaScript. I'm not familiar with Java. Also in the if condition, is it looking by Boolean method? – Kristina Bressler Dec 06 '20 at 08:21
  • @KristinaBressler I've updated it. Sorry I'm also more familiar with JavaScript. But for a weird reason I thought we are talking about Java :D – Aalexander Dec 06 '20 at 09:12
  • 1
    @Alex It's okay. I was able to somehow understand the Java code and converted it to JavaScript code and made some modifications. I'm now figuring out finding hourglass vertical in 6x6 grid. My original question was actually about the && operator, but I used your suggestions to modified my code in different way since I haven't thought about this possibility. There's really lots of ways to solve a javascript challenge. – Kristina Bressler Dec 06 '20 at 10:20
  • On Hackkerrank (it's like leetcode) there are some programming challenges I had a challenge to find the hourglass with the highest sum in an 6x6 grid. This was also really interesting. – Aalexander Dec 06 '20 at 10:25
  • 1
    I just solved this 6x6 grid challenge on HackerRank thanks to your suggestions! – Kristina Bressler Dec 07 '20 at 02:08