2

I feel like I'm close to getting this so just a hint is fine. :) Here is the code, then my question:

function largestOfFour(arr) {
  var matrix = arr;
  var longestNum = 0;
  for (var i = 0; i < matrix.length; i++) {
      for (var j = 0; j < matrix.length; j++)

      if (longestNum < matrix[i][j]) {
          longestNum = matrix[i][j];
      }

      return longestNum; // returns highest value in first sub array       
      //console.log(matrix[i][j]);
  }
      //return longestNum;  //returns highest value in fourth sub array  

}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

The code needs to: Return an array consisting of the largest number from each provided sub-array. My guess at this point would be to something like the following code or am I way off? here is the revised code:

function largestOfFour(arr) {
  var matrix = arr;
  var longestNum = 0;
  for (var i = 0; i < matrix.length; i++) {
      for (var j = 0; j < matrix.length; j++)

      if (longestNum < matrix[i][j]) {
          longestNum = matrix[i][j];
          newArr = [];
            for (var k = 0; k < matrix.length; k++) {
                newArr += longestNum;
            }

      }

      //return [newArr]; // returns highest value in first sub array       
      //console.log(matrix[i][j]);
  }
      return [newArr];  //returns highest value in fourth sub array  

}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

I put return statements to see what it's returning and it shows the first and last highest number so I just need to return all of them.

Ugh, coding is HARD! :D

echo
  • 53
  • 5
  • 1
    you just need save row index and then return `matrix[rowindex]`. Also can you provide expected output for your sample data? – Grundy Aug 29 '15 at 17:26
  • Ok, let me try that. Thanks! I guess sample out would be: [5, 27, 39, 1001] – echo Aug 29 '15 at 17:27
  • this work if you expect `[1000, 1001, 857, 1]` as output for your sample, if not - provide expected output – Grundy Aug 29 '15 at 17:30

2 Answers2

2

In your first solution you had written return statement at the end of inner for loop. that will break for loop and return the largest element from first subarray.

Below is the solution for you problem. just a little modification in your code and it's working perfectly fine.

function largestOfFour(arr) {
    var matrix = arr;
    var longestNum = 0;
    var longestArray = [];
    for (var i = 0; i < matrix.length; i++) {
        longestNum = 0;
        for (var j = 0; j < matrix[i].length; j++){
          if (longestNum < matrix[i][j]) {
              longestNum = matrix[i][j];
          }
        }
        longestArray.push(longestNum); // push highest value in first sub array       
   }
   return longestArray;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
  • Wow! Thank you. I was so close yet sooooo far away. I'm not too familar with `push` so guess I need to get to know it much better. Thanks again. – echo Aug 29 '15 at 17:44
  • that's what i was telling you. you were actually very close to the solution. push does nothing more than appending new element at the end of the array.. and yeah if this works for you than please make is as accepted answer. thanks and welcome :) – Rajendrasinh Parmar Aug 29 '15 at 17:47
  • hey check this updated code as if your array is in revers order previous code wont work properly. – Rajendrasinh Parmar Aug 29 '15 at 17:51
1

Okay so there are a few things wrong here:

First, your nested loop should use matrix[i].length instead of matrix.length (your array sizes may not be equal, and may not be equal to the length of the top level array in instances like: [[0,1,2,3,4],[3,8],[100,4,1]]).

Second, your third loop is not necessary. Since as @Grundy said, you just need to return the rowindex by setting it equal to i inside your if condition

siracoj
  • 101
  • 9