Can't get test passed even though the result is correct

Tell us what’s happening:
can’t get test passed even though the result is correct

Your code so far


function maximumPathSumI(triangle) {
// Good luck!
var copyTriangle = [...triangle];
var dim = triangle.length;
for(var i= dim - 1; i>0; i--){
  for(var j = 0;j < i; j++){
    copyTriangle[i-1][j] += copyTriangle[i][j]>=copyTriangle[i][j+1]? copyTriangle[i][j]:copyTriangle[i][j+1];
  }
  // console.log(copyTriangle);
}
return copyTriangle[0][0];
}

const testTriangle = [[3, 0, 0, 0],
                    [7, 4, 0, 0],
                    [2, 4, 6, 0],
                    [8, 5, 9, 3]];

maximumPathSumI(testTriangle);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Problem 18: Maximum path sum I

Link to the challenge:
https://www.freecodecamp.org/learn/coding-interview-prep/project-euler/problem-18-maximum-path-sum-i

it is resolved if the last line
maximumPathSumI(testTriangle);
is commented out

this can’t copy the inner arrays, so you are changing the global variable anyway.
If you want to do a copy of a multidimensional array you can use JSON.parse(JSON.stringify(arr))

1 Like

Good to know! thanks for sharing this!