Function returns the longest number from each nested array

The code:

function longestfour(arr) {
  let w;
  let newArr = [];
  let nw = 0;
  for (let r in arr) {
    w = arr[r];
    for (let rr in w) {
      if (w[rr] > nw) {
        nw = w[rr];
      }

    }
    newArr.push(nw);
  }
  return newArr;
}
console.log(longestfour([
  [-334, -6, -243, -87],
  [7, 65, 46, 3],
  [8, 55, 76, 2]
]));

The problem: Its not working when nested array elements are negative..!! It should return [-6,65,76]

Answer

This is because your are starting the comparsion with nw=0… Then all negative numbers are less than 0. Only change it to let nw = Number.MIN_SAFE_INTEGER ; then your code works fine.