-1

I can't seem to figure out why the functions returns the "sum" after first for loop but not at the end of the function.

var a = [
  [1, 1, 1, 0, 0, 0],
  [0, 1, 0, 0, 0, 0],
  [1, 1, 1, 0, 0, 0],
  [0, 0, 2, 4, 4, 0],
  [0, 0, 0, 2, 0, 0],
  [0, 0, 1, 2, 4, 0]
];

function hourglassSum(arr) {
  var sum = 0;
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      var sumTemp = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
      if (!isNaN(sumTemp) && sumTemp > sum) {
        sum = sumTemp;
      }
    }
    document.write('Sum: ' + sum + '<br/>');
  }
  document.write('Sum: ' + sum + '<br/>');
}

hourglassSum(a);

4
  • Because you've used document.write statements after the loop bodies end. See this example to learn how to return values from a function. Commented Aug 24, 2018 at 13:18
  • How do you know? Commented Aug 24, 2018 at 13:18
  • 1
    Hi! Please have a look around, and read through the help center, in particular How do I ask a good question? Jon Skeet's question checklist is a good read as well. What is the code supposed to do? What is it about what it's doing instead that's a problem? Also note that since j can be the index of the last entry in arr[i], using arr[i][j + anything_greter_than_zero] will result in undefined. Commented Aug 24, 2018 at 13:19
  • hackerrank.com/challenges/2d-array/problem This is the problem. I understand that I will get undefined, that's why I used isNaN. The function works and returns sum inside the 2nd for loop but it does not return at the end of function. Commented Aug 24, 2018 at 19:12

1 Answer 1

1

Your problem is that you are trying to access an array indexes greater than array's size when calling:

var sumTemp = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];

because you are looping to the array's size and then calling j + 1 or j + 2 which is now greater than the array size and causing an error.

Sign up to request clarification or add additional context in comments.

3 Comments

The code works fine with the first document.write(). its generating undefined but thats ok. I dont have a problem generating numbers, the problem is the second document.write() does not output.
Yup that's what i'm trying to say, because when you are calling arr[i][j+2] on j = arr[i].length - 2 this means you are calling arr[i][arr[i].length] which is greater than array's size and causes an error that leads in getting out of the function and not continuing to the second document.write. If you can tell me what you want to achieve i can fix your code
Ok now I understand. If I retrun a[3][20] it will return undefined but if i go a[20][3] I'll get an error. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.