Search code examples
javascriptblockly

For with Return in Javascript


I want to use the for in a return with the values of the variables dropdown_number and statements_action. However, the code below is not working.

  var dropdown_number = block.getFieldValue('number');
  var statements_action = Blockly.JavaScript.statementToCode(block, 'action');
  // TODO: Assemble JavaScript into code variable.
  return 'for(i = 0; i <' + dropdown_number + '; i++){' + statements_action + '}';

The return must bring the value in statements_action the number of times required in the dropdown_number variable.


Solution

  • As jarmod says you are generationg string. But the answer to your question is that you'll need to store the values you retrieve in a variable and then return it.

    let result;
    for(i = 0; i <' + dropdown_number + '; i++){
        result +=  ' + statements_action + ';
    }
    return result;