Hi,
By design, Blockly's JavaScript code generator globally declares the variables created by Blockly's built-in variable blocks. This is where the var declaration at the top of your code comes from. This is primarily because local scoping is too complex for many users of products that use Blockly.
It appears that the block-code generators for your blocks declare variables (the let and const declarations), which conflicts with Blockly's var declaration. Here are some ideas on how to fix your code, from easiest to hardest:
1) Update your block-code generators so that they assign but do not declare variables. That is, remove the let and const keywords. This would allow Blockly to declare your variables globally without conflicts.
2) Use the
lexical variables plugin from MIT App Inventor. This adds complexity, but allows you to use locally scoped variables.
3) Fork Blockly's JavaScript generator to remove the line that declares variables globally and make sure you declare all your variables. This is not recommended.
Good luck!
Ronald Bourret
Technical Writer (Provided by Synergis)