Skip to content
ChartDeck ChartScript docs Account Open the terminal

CS5302

warning The array is changed inside the loop that goes through it; the loop goes on with its new size. An index loop says that more clearly.

#What it means

This is a warning, shown while you edit. The array a for (const x of ...) loop walks is changed inside the loop (an element is added or removed). The loop follows the array's new size, which is allowed but easy to misread.

#Typical cause

  • push, pop, insert, remove, shift, unshift, clear or concat on the array being walked.

#How to fix it

Use an index loop (for (let i = 0; i < xs.size(); i++)), which makes it clear the size is read again, or collect the changes in a second array and apply them after the loop. See Loops.

#Example

ChartScriptOpen in editor
//@chartscript 1
indicator("Growing list")
const xs = array.from(1.0, 2.0, 3.0)
for (const x of xs) {
    if (x < 3) {
        xs.push(x + 10)
    }
}
plot(xs.size())

Fixed

ChartScriptOpen in editor
//@chartscript 1
indicator("Growing list")
const xs = array.from(1.0, 2.0, 3.0)
const extra = array.new<float>()
for (const x of xs) {
    if (x < 3) {
        extra.push(x + 10)
    }
}
xs.concat(extra)
plot(xs.size())
  • CS2301 — name is the same object on every bar, so name[n] returns the same object.
  • CS5301 — A loop ran more than max times on one bar.

The ChartScript documentation is in English for now; translations will follow.