Type Check using Blockly

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

This demonstration of a type check of an input integer is much more complicated than the preceding validation methods. An input string is accepted if it is convertible to an integer.

Our Blockly program to convert strings to integers was a useful starting point for this demonstration. We changed the code of our function CharToDigit so that it returns -1 if the character cannot be converted to an digit. The new function IntVal is an adaptation of Int. In addition to the string to be converted it takes an integer parameter which is the value returned if not every character in the string is in the range from '0' to '9'.

This demonstration uses more variables than necessary. For example, you could use either strInputNum or strNum rather than both. (We use both to show that it is not necessary to supply a parameter with the same name as that used by the routine).

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="5^%nGyk.xeN/-30P(0M," x="38" y="13">
    <mutation>
      <arg name="strDigit"></arg>
    </mutation>
    <field name="NAME">CharToDigit</field>
    <comment pinned="false" h="80" w="160">Converts single character string to digit</comment>
    <statement name="STACK">
      <block type="variables_set" id="k,6?K]*g_Z-02DtcP=7s">
        <field name="VAR">intDigit</field>
        <value name="VALUE">
          <block type="math_number" id="ee{E87x{tF{L@YM`IpLg">
            <field name="NUM">-1</field>
          </block>
        </value>
        <next>
          <block type="controls_for" id="HxnO|}:0:TV~`KzfpesL">
            <field name="VAR">i</field>
            <value name="FROM">
              <shadow type="math_number" id="NW%K5uv1,x?d~k,}bY#P">
                <field name="NUM">0</field>
              </shadow>
            </value>
            <value name="TO">
              <shadow type="math_number" id="ym.~?ppG17/I@U!mMY;S">
                <field name="NUM">9</field>
              </shadow>
            </value>
            <value name="BY">
              <shadow type="math_number" id="soI4mAw$n7F18o6.dWqM">
                <field name="NUM">1</field>
              </shadow>
            </value>
            <statement name="DO">
              <block type="controls_if" id="85it;Rz?;w(.KuMwXT2W">
                <value name="IF0">
                  <block type="logic_compare" id="KsD;r*8(]*~,Sdb+U@VR">
                    <field name="OP">EQ</field>
                    <value name="A">
                      <block type="text_join" id="ewS+W5u*8SMu-Q/VKL1g">
                        <mutation items="1"></mutation>
                        <value name="ADD0">
                          <block type="variables_get" id="@0)?vn)fL)dwM]j*mSLs">
                            <field name="VAR">i</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <value name="B">
                      <block type="variables_get" id="O]B2[2V_DLa1@MI=v_LD">
                        <field name="VAR">strDigit</field>
                      </block>
                    </value>
                  </block>
                </value>
                <statement name="DO0">
                  <block type="variables_set" id="}YdjkO%y95DyTZj|~Nmw">
                    <field name="VAR">intDigit</field>
                    <value name="VALUE">
                      <block type="variables_get" id=";il@iz8E^SUDtKCo8a`k">
                        <field name="VAR">i</field>
                      </block>
                    </value>
                    <next>
                      <block type="controls_flow_statements" id="#lf8PyTqWEq|ReQnp7IX">
                        <field name="FLOW">BREAK</field>
                      </block>
                    </next>
                  </block>
                </statement>
              </block>
            </statement>
          </block>
        </next>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="TUKBQ+YT._gE[I5[ckCw">
        <field name="VAR">intDigit</field>
      </block>
    </value>
  </block>
  <block type="procedures_defreturn" id="9;Q+leVY@WfvmAxLr{HC" x="38" y="263">
    <mutation>
      <arg name="strNum"></arg>
      <arg name="intInvalid"></arg>
    </mutation>
    <field name="NAME">IntVal</field>
    <comment pinned="false" h="80" w="160">Converts string to integer (to intInvalid if not valid)</comment>
    <statement name="STACK">
      <block type="variables_set" id="_i~3~3Ujvu3EX13ynp:_">
        <field name="VAR">boolValid</field>
        <value name="VALUE">
          <block type="logic_boolean" id="=B6e=tk}8vv)PH)DyyY@">
            <field name="BOOL">TRUE</field>
          </block>
        </value>
        <next>
          <block type="variables_set" id="=uDPxV8r{AdC1gsl!I,U">
            <field name="VAR">boolNeg</field>
            <value name="VALUE">
              <block type="logic_boolean" id="8LHc}~EGmlyI]/%uce$0">
                <field name="BOOL">FALSE</field>
              </block>
            </value>
            <next>
              <block type="controls_if" id="4I2)px`{q4-#b7O=]qeJ">
                <value name="IF0">
                  <block type="logic_compare" id="bL!c9J~@rnK^i%-%l)ty">
                    <field name="OP">EQ</field>
                    <value name="A">
                      <block type="text_charAt" id="#0Tyz~yHt*;6LUk4XP.4">
                        <mutation at="false"></mutation>
                        <field name="WHERE">FIRST</field>
                        <value name="VALUE">
                          <block type="variables_get" id="bKs5xgDn8.u7tRsfT;`W">
                            <field name="VAR">strNum</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <value name="B">
                      <block type="text" id="Ntg^,R6+LAL6!i*i2aDL">
                        <field name="TEXT">-</field>
                      </block>
                    </value>
                  </block>
                </value>
                <statement name="DO0">
                  <block type="variables_set" id="?~23qQ@p%f|P{?*MJbQ[">
                    <field name="VAR">boolNeg</field>
                    <value name="VALUE">
                      <block type="logic_boolean" id="Gu9@)]$.:N6s0!uH~C6r">
                        <field name="BOOL">TRUE</field>
                      </block>
                    </value>
                    <next>
                      <block type="variables_set" id="%#i;g!kp7Cp.G30sDsmY">
                        <field name="VAR">strNum</field>
                        <value name="VALUE">
                          <block type="text_getSubstring" id="]m@N|`El3%QYMug`^#~y">
                            <mutation at1="true" at2="false"></mutation>
                            <field name="WHERE1">FROM_START</field>
                            <field name="WHERE2">LAST</field>
                            <value name="STRING">
                              <block type="variables_get" id="3OIM0YD+;._?U1w|%u8*">
                                <field name="VAR">strNum</field>
                              </block>
                            </value>
                            <value name="AT1">
                              <block type="math_number" id="o9;)C3(`B?7GUWjdOz5(">
                                <field name="NUM">2</field>
                              </block>
                            </value>
                          </block>
                        </value>
                      </block>
                    </next>
                  </block>
                </statement>
                <next>
                  <block type="variables_set" id="xbCuS?wp8mCd8BdR..`g">
                    <field name="VAR">intNum</field>
                    <value name="VALUE">
                      <block type="math_number" id="xSl99;E]+X=a$WpKIz}|">
                        <field name="NUM">0</field>
                      </block>
                    </value>
                    <next>
                      <block type="controls_for" id="D_[!8L+BC0M-76uuP8~c">
                        <field name="VAR">j</field>
                        <value name="FROM">
                          <shadow type="math_number" id="KjupG)DmU8IDAAJmM]OW">
                            <field name="NUM">1</field>
                          </shadow>
                        </value>
                        <value name="TO">
                          <shadow type="math_number" id="/[D!Qt6AY!m=tQXl,rMD">
                            <field name="NUM">10</field>
                          </shadow>
                          <block type="text_length" id="PmPlXS(99^^8TFH2xM6l">
                            <value name="VALUE">
                              <shadow type="text" id="H/W%X7U+(b0A`yL_[F6D">
                                <field name="TEXT">abc</field>
                              </shadow>
                              <block type="variables_get" id="BD5Bit$*[BaifNl6c_uB">
                                <field name="VAR">strNum</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <value name="BY">
                          <shadow type="math_number" id="Z;!xp9KgwPIuLx8c@3-3">
                            <field name="NUM">1</field>
                          </shadow>
                        </value>
                        <statement name="DO">
                          <block type="variables_set" id="y[jS`@RY~/:/`fZ@B$Ho">
                            <field name="VAR">strCurrentChar</field>
                            <value name="VALUE">
                              <block type="text_charAt" id="(gMYZ.$VIED4-Z+@:-Fw">
                                <mutation at="true"></mutation>
                                <field name="WHERE">FROM_START</field>
                                <value name="VALUE">
                                  <block type="variables_get" id="UQC#NYr9^lv^pU_jf*7+">
                                    <field name="VAR">strNum</field>
                                  </block>
                                </value>
                                <value name="AT">
                                  <block type="variables_get" id="v2ae;]BqKZH*{}$C*Evi">
                                    <field name="VAR">j</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <next>
                              <block type="variables_set" id="R45,@kP1HJ:AznL$muS:">
                                <field name="VAR">intMultiplier</field>
                                <value name="VALUE">
                                  <block type="math_round" id="g;X4Mi$.$A/av^DntM-y">
                                    <field name="OP">ROUND</field>
                                    <value name="NUM">
                                      <shadow type="math_number" id="}IE1uPg25@j%X2yO}#V;">
                                        <field name="NUM">3.1</field>
                                      </shadow>
                                      <block type="math_single" id="170Xf$[=iLo)YtP)$/T=">
                                        <field name="OP">POW10</field>
                                        <value name="NUM">
                                          <shadow type="math_number" id="ACXu3$|CE#S3(2;Yc_iv">
                                            <field name="NUM">9</field>
                                          </shadow>
                                          <block type="math_arithmetic" id="V~GG`JMt:w09Q:(y0*?e">
                                            <field name="OP">MINUS</field>
                                            <value name="A">
                                              <shadow type="math_number" id="hLFxm!xa$7*_$c%(`=i/">
                                                <field name="NUM">1</field>
                                              </shadow>
                                              <block type="text_length" id="M,hTTb,qW]]PnE$S)WLS">
                                                <value name="VALUE">
                                                  <shadow type="text" id="WpAh84$}70}fN!gKdRIW">
                                                    <field name="TEXT">abc</field>
                                                  </shadow>
                                                  <block type="variables_get" id="d$5YXZqC9rw;H(o7Qa6/">
                                                    <field name="VAR">strNum</field>
                                                  </block>
                                                </value>
                                              </block>
                                            </value>
                                            <value name="B">
                                              <shadow type="math_number" id="fPo?jzW(g{z[$H_,UI.t">
                                                <field name="NUM">1</field>
                                              </shadow>
                                              <block type="variables_get" id="Y/cnN(Cv!o?^w/Vz`Nt^">
                                                <field name="VAR">j</field>
                                              </block>
                                            </value>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                                <next>
                                  <block type="variables_set" id="X(I?A|6KLwMimsM1etJS">
                                    <field name="VAR">intCurrentDigit</field>
                                    <value name="VALUE">
                                      <block type="procedures_callreturn" id=";mn}FX!+]3kEuBm=e2b,">
                                        <mutation name="CharToDigit">
                                          <arg name="strDigit"></arg>
                                        </mutation>
                                        <value name="ARG0">
                                          <block type="variables_get" id="bU+^2*Br=_DMRi?E~uRm">
                                            <field name="VAR">strCurrentChar</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                    <next>
                                      <block type="controls_if" id="*12}/_Z)wQ{+.[+`k{5W">
                                        <mutation else="1"></mutation>
                                        <value name="IF0">
                                          <block type="logic_compare" id="=OOQNQ.J{Oq)MD5kr!N~">
                                            <field name="OP">GTE</field>
                                            <value name="A">
                                              <block type="variables_get" id="0LO?*|t}]+0F`4%Ok@Sx">
                                                <field name="VAR">intCurrentDigit</field>
                                              </block>
                                            </value>
                                            <value name="B">
                                              <block type="math_number" id="MRUaU{=D)_BXo@#Bd?`r">
                                                <field name="NUM">0</field>
                                              </block>
                                            </value>
                                          </block>
                                        </value>
                                        <statement name="DO0">
                                          <block type="math_change" id="V|kC/A!H+,cwkpczuwZc">
                                            <field name="VAR">intNum</field>
                                            <value name="DELTA">
                                              <shadow type="math_number" id="Fs/`6[nDki~}vEsx=@$+">
                                                <field name="NUM">1</field>
                                              </shadow>
                                              <block type="math_arithmetic" id="DSPs#={C_P:R,Fi)KK;p">
                                                <field name="OP">MULTIPLY</field>
                                                <value name="A">
                                                  <shadow type="math_number" id="V+0rA0c`3p,,O7GuzOr-">
                                                    <field name="NUM">1</field>
                                                  </shadow>
                                                  <block type="variables_get" id="UbxJzmk6zebJ+b.@u2+O">
                                                    <field name="VAR">intCurrentDigit</field>
                                                  </block>
                                                </value>
                                                <value name="B">
                                                  <shadow type="math_number" id="o)VARrFspGY!N-8-eDd[">
                                                    <field name="NUM">1</field>
                                                  </shadow>
                                                  <block type="variables_get" id="MYpeSy/@Ts3AeuIXE_RZ">
                                                    <field name="VAR">intMultiplier</field>
                                                  </block>
                                                </value>
                                              </block>
                                            </value>
                                          </block>
                                        </statement>
                                        <statement name="ELSE">
                                          <block type="variables_set" id="RswBZRj9xwFmZen[Vi9l">
                                            <field name="VAR">boolValid</field>
                                            <value name="VALUE">
                                              <block type="logic_boolean" id=",].Dj;yGNbu5Bu_M|=-P">
                                                <field name="BOOL">FALSE</field>
                                              </block>
                                            </value>
                                          </block>
                                        </statement>
                                      </block>
                                    </next>
                                  </block>
                                </next>
                              </block>
                            </next>
                          </block>
                        </statement>
                        <next>
                          <block type="controls_if" id="df)c~RyqKy0k7+5mfG:c">
                            <value name="IF0">
                              <block type="logic_compare" id="^e6@pclg3Wd9@3^e0xK=">
                                <field name="OP">EQ</field>
                                <value name="A">
                                  <block type="variables_get" id=",^F.=z!NEGDL+Gl8rB!}">
                                    <field name="VAR">boolNeg</field>
                                  </block>
                                </value>
                                <value name="B">
                                  <block type="logic_boolean" id="iQC][*sB8ah4pP[_4kOh">
                                    <field name="BOOL">TRUE</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <statement name="DO0">
                              <block type="variables_set" id="RP__.C|IsZjO=c{;I1P8">
                                <field name="VAR">intNum</field>
                                <value name="VALUE">
                                  <block type="math_single" id="o8%vcq+|0sDSUErX3v.G">
                                    <field name="OP">NEG</field>
                                    <value name="NUM">
                                      <shadow type="math_number" id="X;6MH[y^SK5v}p,EhZ[U">
                                        <field name="NUM">9</field>
                                      </shadow>
                                      <block type="variables_get" id="WJAP[E~z)/F0_/MO;4uu">
                                        <field name="VAR">intNum</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                              </block>
                            </statement>
                            <next>
                              <block type="controls_if" id="y!tGUixft_,0[8=rF/1[">
                                <value name="IF0">
                                  <block type="logic_compare" id=":pG[JbD8hGP!RjYHHEmC">
                                    <field name="OP">EQ</field>
                                    <value name="A">
                                      <block type="variables_get" id="hX[Or*9ixj)e:9-R)!KO">
                                        <field name="VAR">boolValid</field>
                                      </block>
                                    </value>
                                    <value name="B">
                                      <block type="logic_boolean" id="gEmXcwn;?5c/}X_,037h">
                                        <field name="BOOL">FALSE</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                                <statement name="DO0">
                                  <block type="variables_set" id=":o;G0msEY4,fFi((bwvT">
                                    <field name="VAR">intNum</field>
                                    <value name="VALUE">
                                      <block type="variables_get" id="[1Ia0ARM=C|z/=4j1!{D">
                                        <field name="VAR">intInvalid</field>
                                      </block>
                                    </value>
                                  </block>
                                </statement>
                              </block>
                            </next>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="7sr*pnw%AVvV_wOC%sN)">
        <field name="VAR">intNum</field>
      </block>
    </value>
  </block>
  <block type="variables_set" id="5:+4j_Y{6?EWt/=AQOm8" x="38" y="988">
    <field name="VAR">boolValidated</field>
    <value name="VALUE">
      <block type="logic_boolean" id="icMLzW(@rbiYAnUc4Pm|">
        <field name="BOOL">FALSE</field>
      </block>
    </value>
    <next>
      <block type="controls_whileUntil" id="J:@;B4oJ,kmQz[}+#^3)">
        <field name="MODE">WHILE</field>
        <value name="BOOL">
          <block type="logic_compare" id="9%?M+g-agcj{lXJ_!M/{">
            <field name="OP">EQ</field>
            <value name="A">
              <block type="variables_get" id="X]2N6bxeTS_`.Zr}T3Ar">
                <field name="VAR">boolValidated</field>
              </block>
            </value>
            <value name="B">
              <block type="logic_boolean" id="mNL#[lGF|/cFZYkiZk0t">
                <field name="BOOL">FALSE</field>
              </block>
            </value>
          </block>
        </value>
        <statement name="DO">
          <block type="variables_set" id="@ttfVpxxe;!mF8VmNBE8">
            <field name="VAR">strInputNum</field>
            <value name="VALUE">
              <block type="text_prompt_ext" id="!hR(){dVo8/sl7*Gx$o4">
                <mutation type="TEXT"></mutation>
                <field name="TYPE">TEXT</field>
                <value name="TEXT">
                  <shadow type="text" id="z75={LK,c94)icK7`N06">
                    <field name="TEXT">Please enter an integer</field>
                  </shadow>
                </value>
              </block>
            </value>
            <next>
              <block type="variables_set" id="{JOSt~m0MvG3l#+V*Eza">
                <field name="VAR">intResult</field>
                <value name="VALUE">
                  <block type="procedures_callreturn" id="e1^V]2HtqqaP)yASLo)$">
                    <mutation name="IntVal">
                      <arg name="strNum"></arg>
                      <arg name="intInvalid"></arg>
                    </mutation>
                    <value name="ARG0">
                      <block type="variables_get" id="$P)|Y6~NJj|?THq]NG~W">
                        <field name="VAR">strInputNum</field>
                      </block>
                    </value>
                    <value name="ARG1">
                      <block type="math_number" id="[1H7K:w4@[^$wFYDzf~A">
                        <field name="NUM">-999</field>
                      </block>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="controls_if" id="r#CuRg!vVL#qr;{QsvTk">
                    <value name="IF0">
                      <block type="logic_compare" id="HvmvX:ep[EOhM!2J..~N">
                        <field name="OP">NEQ</field>
                        <value name="A">
                          <block type="variables_get" id="LdgvbM[8WA`$4*3n$r((">
                            <field name="VAR">intResult</field>
                          </block>
                        </value>
                        <value name="B">
                          <block type="math_number" id="`obcilCyMh!*ZSM5L.RW">
                            <field name="NUM">-999</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <statement name="DO0">
                      <block type="variables_set" id="nE#B[w5YHpHBZC%Cl}de">
                        <field name="VAR">boolValidated</field>
                        <value name="VALUE">
                          <block type="logic_boolean" id="TQ~uC^{*q{.wJFzBB::M">
                            <field name="BOOL">TRUE</field>
                          </block>
                        </value>
                      </block>
                    </statement>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </statement>
        <next>
          <block type="text_print" id="AmLKZLGsb%6_{tMIPctq">
            <value name="TEXT">
              <shadow type="text" id="llb=zfN#hYqkwLI[(19C">
                <field name="TEXT">abc</field>
              </shadow>
              <block type="text_join" id="`4fV=ikikqvxPXyh=VFP">
                <mutation items="2"></mutation>
                <value name="ADD0">
                  <block type="text" id="aU@A_I}X*T;EuQPI]Yk}">
                    <field name="TEXT">Validated integer: </field>
                  </block>
                </value>
                <value name="ADD1">
                  <block type="variables_get" id="SN#.E!2?.EdV4?K3AltD">
                    <field name="VAR">intResult</field>
                  </block>
                </value>
              </block>
            </value>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var intDigit: Integer;
var strDigit: String;
var intNum: Integer;
var intInvalid: Integer;
var boolValid: Boolean;
var boolNeg: Boolean;
var intMultiplier: Integer;
var intCurrentDigit: Integer;
var strCurrentChar: String;
var strNum: String;
var intResult: Integer;
var boolValidated: Boolean;
var strInputNum: String;


function Str(v: Variant) : string;
begin
  asm
    if (@v === 0) {
      @Result = '0';
    }
    else if (@v === '') {
      @Result = '';
    }
    else if (isNaN(@v)) {
      @Result = @v;
    }
    else {
      @Result = (@v).toString();
    }
  end;
end;


{
  Converts single character string to digit
}
function CharToDigit(strDigit: String): Integer;
begin
  intDigit := -1;
  for var i := 0 to 9 do
  begin
    if ((Str(i) = strDigit)) then
    begin
      intDigit := i;
      break;
    end;
  end;
  Result := intDigit;
end;


{
  Converts string to integer (to intInvalid if not valid)
}
function IntVal(strNum: String; intInvalid: Integer): Integer;
begin
  boolValid := true;
  boolNeg := false;
  if ((LeftStr(strNum, 1) = '-')) then
  begin
    boolNeg := true;
    strNum := copy(strNum, 1 + 1, strNum.length - 1);
  end;
  intNum := 0;
  for var j := 1 to length(strNum) step 1 do
  begin
    strCurrentChar := CharAt(strNum, (j - 1) + 1);
    intMultiplier := Round(Power(10,length(strNum) - j));
    intCurrentDigit := CharToDigit(strCurrentChar);
    if ((intCurrentDigit >= 0)) then
    begin
      intNum += intCurrentDigit * intMultiplier;
    end
    else
    begin
      boolValid := false;
    end;
  end;
  if ((boolNeg = true)) then
  begin
    intNum := -intNum;
  end;
  if ((boolValid = false)) then
  begin
    intNum := intInvalid;
  end;
  Result := intNum;
end;



boolValidated := false;
while ((boolValidated = false)) do
begin
  strInputNum := prompt('Please enter an integer');
  intResult := IntVal(strInputNum, -999);
  if ((intResult <> -999)) then
  begin
    boolValidated := true;
  end;
end;
Console.writeln(Str('Validated integer: ') + Str(intResult));
    

Generated JavaScript

var intDigit, i, strDigit, intNum, intInvalid, boolValid, boolNeg, intMultiplier, intCurrentDigit, strCurrentChar, j, strNum, intResult, boolValidated, strInputNum;

/**
 * Converts single character string to digit
 */
function CharToDigit(strDigit) {
  intDigit = -1;
  for (i = 0; i <= 9; i++) {
    if (String(i) == strDigit) {
      intDigit = i;
      break;
    }
  }
  return intDigit;
}

/**
 * Converts string to integer (to intInvalid if not valid)
 */
function IntVal(strNum, intInvalid) {
  boolValid = true;
  boolNeg = false;
  if (strNum.charAt(0) == '-') {
    boolNeg = true;
    strNum = strNum.slice(1, strNum.length);
  }
  intNum = 0;
  var j_end = strNum.length;
  var j_inc = 1;
  if (1 > j_end) {
    j_inc = -j_inc;
  }
  for (j = 1; j_inc >= 0 ? j <= j_end : j >= j_end; j += j_inc) {
    strCurrentChar = strNum.charAt((j - 1));
    intMultiplier = Math.round(Math.pow(10,strNum.length - j));
    intCurrentDigit = CharToDigit(strCurrentChar);
    if (intCurrentDigit >= 0) {
      intNum = (typeof intNum == 'number' ? intNum : 0) + intCurrentDigit * intMultiplier;
    } else {
      boolValid = false;
    }
  }
  if (boolNeg == true) {
    intNum = -intNum;
  }
  if (boolValid == false) {
    intNum = intInvalid;
  }
  return intNum;
}


boolValidated = false;
while (boolValidated == false) {
  strInputNum = window.prompt('Please enter an integer');
  intResult = IntVal(strInputNum, -999);
  if (intResult != -999) {
    boolValidated = true;
  }
}
window.alert(String('Validated integer: ') + String(intResult));

Generated Python Code

import math
from numbers import Number

intDigit = None
i = None
strDigit = None
intNum = None
intInvalid = None
boolValid = None
boolNeg = None
intMultiplier = None
intCurrentDigit = None
strCurrentChar = None
j = None
strNum = None
intResult = None
boolValidated = None
strInputNum = None

"""Converts single character string to digit
"""
def CharToDigit(strDigit):
  global intDigit, i, intNum, intInvalid, boolValid, boolNeg, intMultiplier, intCurrentDigit, strCurrentChar, j, strNum, intResult, boolValidated, strInputNum
  intDigit = -1
  for i in range(10):
    if str(i) == strDigit:
      intDigit = i
      break
  return intDigit

def upRange(start, stop, step):
  while start <= stop:
    yield start
    start += abs(step)

def downRange(start, stop, step):
  while start >= stop:
    yield start
    start -= abs(step)

"""Converts string to integer (to intInvalid if not valid)
"""
def IntVal(strNum, intInvalid):
  global intDigit, i, strDigit, intNum, boolValid, boolNeg, intMultiplier, intCurrentDigit, strCurrentChar, j, intResult, boolValidated, strInputNum
  boolValid = True
  boolNeg = False
  if strNum[0] == '-':
    boolNeg = True
    strNum = strNum[1 : ]
  intNum = 0
  j_end = float(len(strNum))
  for j in (1 <= j_end) and upRange(1, j_end, 1) or downRange(1, j_end, 1):
    strCurrentChar = strNum[int(j - 1)]
    intMultiplier = round(math.pow(10,len(strNum) - j))
    intCurrentDigit = CharToDigit(strCurrentChar)
    if intCurrentDigit >= 0:
      intNum = (intNum if isinstance(intNum, Number) else 0) + intCurrentDigit * intMultiplier
    else:
      boolValid = False
  if boolNeg == True:
    intNum = -intNum
  if boolValid == False:
    intNum = intInvalid
  return intNum

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


boolValidated = False
while boolValidated == False:
  strInputNum = text_prompt('Please enter an integer')
  intResult = IntVal(strInputNum, -999)
  if intResult != -999:
    boolValidated = True
print(str('Validated integer: ') + str(intResult))


Copy of Output

After attempting to enter the values a42, 42a, 3.142 and 123456 the eventual outcome was:

Validated integer: 123456
Programming - a skill for life!

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