Troubleshoot: Return Largest Numbers in Arrays

I will really learn this better by having someone help trouble shoot the code I wrote rather than looking up the full solution. Thanks in advance for the help!

  // You can do this!
  let bigPieces = [ ];
  let pieces = [ ];
  let chunkyMonkey = 0;
  for (let i = 0; i < arr.length; i++){
    pieces = [...i];
    for (let j = 0; j < pieces.length; j++){
      if (j > chunkyMonkey){
      chunkyMonkey = pieces[j];
      }
      bigPieces.push(chunkyMonkey);
    }
  }
  return bigPieces;
}

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

first I must say descriptive names for variables make code far more readable, and so much easier to troubleshoot

first subarray, chunkyMonkey is 5, and it is pushed to bigPieces

what’s the value of chunkyMonkey when the code starts iterating over the second subarray?

Ok, I see what you mean, both regarding names and resetting the chunkyMonkey variable. How do I/ where is the lesson (to refresh my memory) to fix it? I changed the names, I hope that helps everyone else that looks at this, chunkyMonkey is now bigValue.

  // You can do this!
  let bigPieces = [ ];
  let pieces = [ ];
  let bigValue = 0;
  for (let i = 0; i < arr.length; i++){
    pieces = [...i];
    for (let j = 0; j < pieces.length; j++){
      if (j > bigValue){
      bigValue = pieces[j];
      }
      bigPieces.push(bigValue);
    }
  }
  return bigPieces;
}```

here there is a similar issue:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/debugging/use-caution-when-reinitializing-variables-inside-a-loop

Ok, I have moved the bigValue variable inside the first loop, but it still isn’t resetting properly. This is my code as it stands now.

function largestOfFour(arr) {
  // You can do this!
  let bigPieces = [ ];
  let pieces = [ ];
  for (let i = 0; i < arr.length; i++){
     pieces = [...i];
     let bigValue = 0;
    for (let j = 0; j < pieces.length; j++){
      if (j > bigValue){
      bigValue = pieces[j];
      }
    }
    bigPieces.push(bigValue);
  }
  return bigPieces;
}

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

it is resetting properly, the issue is an other… what happens if the biggest number is negative?

As I look at it; I have a problem any time the largest number in the second sub-array is less than the largest number in the first sub-array. And also negative numbers.

this was fixed when you moved the bigValue declaration in the loop, so it is initialised at 0 for each subarray

the issue is really the other one, it can’t give the right answer if the bigger value is smaller than the starting value of bigValue

and what should this be? i is a number

this is the issue for why nothing work, you are not iterating over the subarray, but on an array of length 1

you can look at the code with this if you don’t believe me Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code

I believe you. pieces[…i] should spread the sub-array at position i into the new array pieces. Did i not organize the syntax for that correctly?

The i is just an incrementing number, there is nothing to spread (it is not iterable). You want to spread the array, not the index into the array.

Ok, so how to spread the sub-array at index i? Changing
pieces = [...i];
to
pieces = [...arr[i]];
hasn’t fixed it.

That is the correct syntax. You have some other bug in your code.

I would suggest using the browser console and the debugger. You can add debugger; to the top of your function to trigger a breakpoint. Step through the code and see if you can find the issue.

Try looking at this part of the code:

for (let j = 0; j < pieces.length; j++){
  if (j > bigValue){
  bigValue = pieces[j];
  }
}

Ok, so I ran through what I have step, by step on python tutor. The first test array went through perfectly and got the expected result. The second test array went through arr[0] and arr[1] correctly, but then pulled the second lowest value from arr[2], and then the correct answer from arr[3]. The correct answer should be [27, 5, 39, 1001], instead I am getting [27, 5, 35, 1001]. This is an odd issue and separate from the negative number problem that I still have to solve.

ah, here, what is j? is it the value you want to use for the comparison?

Yes, j is the value at a given location in the pieces array.

check where j is defined, and how it changes
are you sure it is the value in the array?

if it is the value in the array why then you do bigValue = pieces[j]; and not bigValue = j?

That is what I had originally (see above). I was experimenting with a different way to deal with negative values. It doesn’t work. Still having trouble with the values of pieces[j] not being evaluated correctly in some arrays, but not all. It is very frustrating. Is this a viable solution worth debugging, or should I just scrap it and start over?

it is a viable solution worth debugging but if you made changes you should repost, I can base only on what I see

This is the code as it stands now. It works perfectly and returns the correct result with the first set of arguments. It does not evaluate the second or third set of arguments correctly, before we even get to the negative numbers.

  let bigPieces = [ ];
  let pieces = [ ];
  for (let i = 0; i < arr.length; i++){
     pieces = [...arr[i]];
     let bigValue = 0;
    for (let j = 0; j < pieces.length; j++){
      if (j > bigValue){
      bigValue = pieces[j];
      }
    }
    bigPieces.push(bigValue);
  }
  return bigPieces;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);```