Hi Natalie,
(1) When no variable with that name exists, getVariable will return null so the body of the if statement which sets the variable block won't be run. If you want the user to be able to create new variables as well, you can modify what I wrote as follows that will create a new variable with that name if it doesn't exist:
const variableName = document.getElementById('variable_name_textarea').value;
const variableModel = workspace.getVariable(variableName);
const variableId = (variableModel || workspace.createVariable(variableName)).getId();
variableSetBlock.setFieldValue(variableId, 'VAR');
(3) Yes, you can loop through all of the elements in the Array to do this, essentially using the code from before in the loop body, adding a statement to connect previous/next connections (and setting the variableName to the Array elements instead).
const workspace = Blockly.getMainWorkspace();
const variableNames = ["a", "b", "c"];
const variableSetBlocks = [];
for(let i = 0; i < variableNames.length; i++) {
const variableModel = workspace.getVariable(variableNames[i]);
const variableId = (variableModel || workspace.createVariable(variableNames[i])).getId();
const mathNumberBlock = workspace.newBlock('math_number');
variableSetBlocks.push(workspace.newBlock('variables_set'));
variableSetBlocks[i].initSvg();
variableSetBlocks[i].setFieldValue(variableId, 'VAR');
if(i > 0) {
variableSetBlocks[i].previousConnection.connect(variableSetBlocks[i-1].nextConnection);
}
mathNumberBlock.setFieldValue(document.getElementById('number_textarea').value, 'NUM');
mathNumberBlock.initSvg();
variableSetBlocks[i].getInput('VALUE').connection.connect(mathNumberBlock.outputConnection);
}
workspace.render();
Best,
Jason