Length Check using Blockly

[XML] [Smart Pascal] [JavaScript] [Python] [output]

This is a straightforward demonstration of a length check of an input string. An input string is accepted only if it has the required length. We used the Blockly Playground to generate JavaScript and Python code from the same blocks.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="procedures_defreturn" id="c{hZ9?9l~|xD{@a(#B]k" x="-37" y="-187">
    <mutation>
      <arg name="strInput"></arg>
      <arg name="intInput"></arg>
    </mutation>
    <field name="NAME">LengthCheck</field>
    <comment pinned="false" h="80" w="160">Returns true when length of input string equals input integer.</comment>
    <statement name="STACK">
      <block type="controls_if" id="fZ/e{rdTy?2ARSE7Ldlp">
        <mutation else="1"></mutation>
        <value name="IF0">
          <block type="logic_compare" id="-={RMBQ%6_Yva`cTtL*.">
            <field name="OP">EQ</field>
            <value name="A">
              <block type="text_length" id="j2_1pZXrf8Xn@Qu:x;oj">
                <value name="VALUE">
                  <shadow type="text" id="1i#2q`1e30!l-g#f!~vo">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="variables_get" id="(16s~lBuMx2%R)X%)v2+">
                    <field name="VAR">strInput</field>
                  </block>
                </value>
              </block>
            </value>
            <value name="B">
              <block type="variables_get" id="l^vI*l!.BJ3_4#(+2tby">
                <field name="VAR">intInput</field>
              </block>
            </value>
          </block>
        </value>
        <statement name="DO0">
          <block type="variables_set" id="q|Cm09+tbvFT7AHmAAm}">
            <field name="VAR">boolLengthOK</field>
            <value name="VALUE">
              <block type="logic_boolean" id="=7OJbX|;^8nS-TO[@`18">
                <field name="BOOL">TRUE</field>
              </block>
            </value>
          </block>
        </statement>
        <statement name="ELSE">
          <block type="variables_set" id="IKr.pHswcG+)#h)/Dfcp">
            <field name="VAR">boolLengthOK</field>
            <value name="VALUE">
              <block type="logic_boolean" id="Y=dRyN]t~sAF5JbRzMP7">
                <field name="BOOL">FALSE</field>
              </block>
            </value>
          </block>
        </statement>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="AjYQAT]T[FZ;r1synNCW">
        <field name="VAR">boolLengthOK</field>
      </block>
    </value>
  </block>
  <block type="variables_set" id="ph%FZhEod.3P:?B2ZZ$x" x="-37" y="-12">
    <field name="VAR">boolLengthOK</field>
    <value name="VALUE">
      <block type="logic_boolean" id="wN+MydZ[=qswYlkV6=Y_">
        <field name="BOOL">FALSE</field>
      </block>
    </value>
    <next>
      <block type="variables_set" id="M+g)Folp:ccv%/U0ufjo">
        <field name="VAR">intRequiredLength</field>
        <value name="VALUE">
          <block type="math_number" id="V/1+5}5BGg1QO-jNrpWP">
            <field name="NUM">3</field>
          </block>
        </value>
        <next>
          <block type="controls_whileUntil" id="T[k$3,i%h9lJ4gZDkR(E">
            <field name="MODE">WHILE</field>
            <value name="BOOL">
              <block type="logic_compare" id="Oua{7kw%Z+x6R5_qy_A]">
                <field name="OP">EQ</field>
                <value name="A">
                  <block type="variables_get" id="d`hj|9*[1CdZm2SE@/R?">
                    <field name="VAR">boolLengthOK</field>
                  </block>
                </value>
                <value name="B">
                  <block type="logic_boolean" id="|bg+Qb5a.5a4pwbaQhG_">
                    <field name="BOOL">FALSE</field>
                  </block>
                </value>
              </block>
            </value>
            <statement name="DO">
              <block type="variables_set" id=";cPD@/My(80W7)Q6c1aZ">
                <field name="VAR">strLengthTest</field>
                <value name="VALUE">
                  <block type="text_prompt_ext" id="Q}R?l:)ucdW.zhjMNzu_">
                    <mutation type="TEXT"></mutation>
                    <field name="TYPE">TEXT</field>
                    <value name="TEXT">
                      <shadow type="text" id="V7CcyxnXvO~e.N?gF3@r">
                        <field name="TEXT">Please enter a three character code.</field>
                      </shadow>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="variables_set" id="G=TwqKVVdDj(AGM{e}HU">
                    <field name="VAR">boolLengthOK</field>
                    <value name="VALUE">
                      <block type="procedures_callreturn" id="hE@3WOl~h95RWsCZy0;W">
                        <mutation name="LengthCheck">
                          <arg name="strInput"></arg>
                          <arg name="intInput"></arg>
                        </mutation>
                        <value name="ARG0">
                          <block type="variables_get" id="c96(Bq~{Jx7ox#=1x%Cc">
                            <field name="VAR">strLengthTest</field>
                          </block>
                        </value>
                        <value name="ARG1">
                          <block type="variables_get" id="4h)NO[Kk:t^yY{bt)I_O">
                            <field name="VAR">intRequiredLength</field>
                          </block>
                        </value>
                      </block>
                    </value>
                  </block>
                </next>
              </block>
            </statement>
            <next>
              <block type="text_append" id="DJ/^1q:%AyC2`8aUBwnz">
                <field name="VAR">strLengthTest</field>
                <value name="TEXT">
                  <shadow type="text" id="2Ah[Ndl*=y6nB@ldV4Cp">
                    <field name="TEXT"> accepted</field>
                  </shadow>
                </value>
                <next>
                  <block type="text_print" id="cXd=,olFETYh[(7!!25,">
                    <value name="TEXT">
                      <shadow type="text" id="uS[g/N?5W!Th74aNN5Q]">
                        <field name="TEXT">abc</field>
                      </shadow>
                      <block type="variables_get" id="UQn.}+^mp5s#TjT)1tw0">
                        <field name="VAR">strLengthTest</field>
                      </block>
                    </value>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var boolLengthOK: Boolean;
var intInput: Integer;
var intRequiredLength: Integer;
var strInput: String;
var strLengthTest: String;


{
  Returns true when length of input string equals input integer.
}
function LengthCheck(strInput: String; intInput: Integer): Boolean;
begin
  if ((length(strInput) = intInput)) then
  begin
    boolLengthOK := true;
  end
  else
  begin
    boolLengthOK := false;
  end;
  Result := boolLengthOK;
end;



boolLengthOK := false;
intRequiredLength := 3;
while ((boolLengthOK = false)) do
begin
  strLengthTest := prompt('Please enter a three character code.');
  boolLengthOK := LengthCheck(strLengthTest, intRequiredLength);
end;
strLengthTest += ' accepted';
Console.writeln(strLengthTest);
    

Generated JavaScript

var boolLengthOK, intInput, strInput, strLengthTest, intRequiredLength;

/**
 * Returns true when length of input string equals input integer.
 */
function LengthCheck(strInput, intInput) {
  if (strInput.length == intInput) {
    boolLengthOK = true;
  } else {
    boolLengthOK = false;
  }
  return boolLengthOK;
}


boolLengthOK = false;
intRequiredLength = 3;
while (boolLengthOK == false) {
  strLengthTest = window.prompt('Please enter a three character code.');
  boolLengthOK = LengthCheck(strLengthTest, intRequiredLength);
}
strLengthTest = String(strLengthTest) + String(' accepted');
window.alert(strLengthTest);

Generated Python Code

boolLengthOK = None
intInput = None
strInput = None
strLengthTest = None
intRequiredLength = None

"""Returns true when length of input string equals input integer.
"""
def LengthCheck(strInput, intInput):
  global boolLengthOK, strLengthTest, intRequiredLength
  if len(strInput) == intInput:
    boolLengthOK = True
  else:
    boolLengthOK = False
  return boolLengthOK

def text_prompt(msg):
  try:
    return raw_input(msg)
  except NameError:
    return input(msg)


boolLengthOK = False
intRequiredLength = 3
while boolLengthOK == False:
  strLengthTest = text_prompt('Please enter a three character code.')
  boolLengthOK = LengthCheck(strLengthTest, intRequiredLength)
strLengthTest = str(strLengthTest) + str(' accepted')
print(strLengthTest)

Copy of Output

After attempting to enter the values Q, QW, QWER, QWE the eventual outcome was:

QWE accepted
Programming - a skill for life!

Introduction to validation (length check, range check, presence check, type check and character check)