Question of how to script a for loop

Sorry to bother you again on here, but was trying to script my first 'for' loop in javascript to try and code a bit more efficiently but have run into problems! Just trying to iterate a loop 4 times to save writing it all out but am a bit out of my depth (as usual!). Cannot work out the correct way to get something like this to run:

for (let i = 0; i < 4; i++) {

if (Text+i < 5) {
setVar('KK@{parent.variables.a}DynChannelSelect', 'textColor'+i, '#6ffcd8')
} else
if (Text+i > 4 && Text+i < 9) {
setVar('KK@{parent.variables.a}DynChannelSelect', 'textColor'+i, '#8efd6d')
} else
if (Text+i > 8 && Text+i < 13) {
setVar('KK@{parent.variables.a}DynChannelSelect', 'textColor'+i, '#ecfd6d')
} else
if (Text+i > 12 && Text+i < 17) {
setVar('KK@{parent.variables.a}DynChannelSelect', 'textColor'+i, '#fdc66d')
}
}

Apart from the issues with the 'for' loop, I am not sure that Text+i for the variable is even correct! Apologies that this is all basic stuff, but any help would be greatly appreciated!

Hi,

First set Debug to true in the launcher window. Then watch for messages into the console.
If you want to test , add in your codeconsole.log(Text+i).

If Text is a string then Text+i will be a string too, which you can't compare to a number.

Also: don't use @{} in scripts (we talked about this recently)

Thank you both for replying, really appreciate it. Greenman, that is a great point, I will definitely refer to console log to check if the variable is getting named correctly (ie Text1). Jean Emmanuel, apologies for using @{} again in script, it is a bad habit leftover from when I first delved into OSC!:grin:

The variable 'Text1' in my script refers to a number not a string (I realise now that was a terrible name for a variable referencing a number!). The script above works correctly if I don't try and use a for loop (just using Text1, text2 etc), just trying to extend my programming chops (and failing) as I just need this code to run five times in a row! Can just long hand it though (don't know whether it is less optimized than the for loop that way though!). Thanks for taking the time to have a look at it though, I'll get there! :+1: