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]]);```
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;
}```
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.
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.
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?
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]]);```