Code doesn't past the test and I don't know why

Tell us what’s happening:
Hi, I’m try to run this code but when it run the test doesn’t work in second stage

  **Your code so far**

function maximumPathSumI(triangle) {
let newTriangleArr = [...triangle]
let row
let column

for(row = newTriangleArr.length - 2; 0 <= row; row--){
  for(column = 0; column < newTriangleArr[0].length; column++){
    let cell = newTriangleArr[row][column]
    if(cell === 0) break
    let sum
    let right = newTriangleArr[row + 1][column] + cell
    let left = newTriangleArr[row + 1][column + 1] + cell
    if (right > left){
    sum = right
     } else {
    sum = left
    }
    newTriangleArr[row][column] = sum
  }
}
return newTriangleArr[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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50

Challenge: Problem 18: Maximum path sum I

Link to the challenge:

function maximumPathSumI(triangle) {
  let newTriangleArr = [...triangle];

  for (let row = newTriangleArr.length - 2; 0 <= row; row--) {
    for (let column = 0; column < newTriangleArr[0].length; column++) {
      const cell = newTriangleArr[row][column];
      if (cell === 0) break;
      const right = newTriangleArr[row + 1][column] + cell
      const left  = newTriangleArr[row + 1][column + 1] + cell
      const sum   = (right > left) ? right : left;
      newTriangleArr[row][column] = sum;
    }
  }

  return newTriangleArr[0][0];
}

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

console.log(maximumPathSumI(testTriangle));
console.log(testTriangle);

You’re mutating the input data. Take a look at this console.log

2 Likes

Thanks! that’s the problem

1 Like