Project Euler Problems 1 to 100 - Problem 12: Highly divisible triangular number

Tell us what’s happening:

Here, my results are correct, but I have some optimization issues (when I check the code, an error message is logged : “Potential infinite loop detected on line 2. Tests may fail if this is not changed.”) I commented out everything that helped me for debugging, but I don’t know how to optimize my code any further.

Your code so far

function divisibleTriangleNumber(n) {
  for (let i = 1; true; i++) {
    let res = 0
    let arr = []
    //let printed =  `0 `

    for (let j = 1; j <= i; j++) {
      res += j
      //printed += `+ ${j} `
    }
    //printed += `= ${res}`
    //console.log(printed)
    for (let k = 0; k <= res; k++) {
      if (res % k === 0) {
        arr.push(k)
      }
    }
    //console.log(`Is ${JSON.stringify(arr)} length superior to ${n} ? ${arr.length} > ${n} : ${arr.length > n}`)
    if (arr.length > n) {
      return res
    }
  }
}

//console.log(divisibleTriangleNumber(5));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0

Challenge Information:

Project Euler Problems 1 to 100 - Problem 12: Highly divisible triangular number