Basic Algorithm Scripting -- challenge Feedback Request

Hello, all.

I am posting to ask for general feedback and to see if any of you can help me to understand why this code isn’t passing the related challenge. The callenge can be viewed here: Basic Algorithm Scripting: Return Largest Numbers in Arrays

When I run the code that I initially wrote for the challenge it seems to work as intended (reducing the subarrays to the largest integer in each) but does not pass the tests. I am hoping that you can provide me with insight into why it doesn’t pass the challenge as well as any general input you may have on the way that I have structured this so that I can improve my thinking on the subject.


function largestOfFour(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i][0] > arr[i][1] && arr[i][0] > arr[i][2] && arr[i][0] > arr[i][3]) {
      arr[i].splice(1,3);
    } else {
      arr[i].shift();
      for (let j = 0; j < arr[i].length; j++) {
        if (arr[i][0] > arr[i][1] && arr[i][0] > arr[i][2]) {
          arr[i].splice(1,2);
        } else {
          arr[i].shift();
          if (arr[i][0] > arr[i][1]) {
            arr[i].pop();
          } else {
            arr[i].shift();
          }
        }
      }
    }
  }
  console.log(arr);
  return arr;
}

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

Thank you for taking a look!

Your code returns an array that contains four subarrays of one item. The challenge wants a single array of four items.

As for the overall structure, think about how you can accomplish this with .map and .reduce and your logic will become much simpler.

2 Likes

Thank you very much!