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,clearorconcaton 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
//@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
//@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())