Q:

how to convert a javascript function using charCodeAt to Coldfusion?

I have a javascript function, which I need to run serverside using Coldfusion8.
The javascript function uses charCodeAt to check entered values (ILN, 13-digit values, for example 1234567891231) and computes a checkVal:
checker=0;
for (i=11; i>0; i=i-2){
    checker += (iln.charCodeAt(i)-48)*3;
    checker += iln.charCodeAt(i-1)-48;
    }
checkVal = 10 - (checker%10);
if(checkVal==10){
    checkVal=0;
    }
if(checkVal != iln.charAt(12)){
    alert("error, should be" + checkVal);
    }

In Coldfusion I'm trying to do this with a loop, mid() and asc(), but I can't get it to work. Here is what I have:
<cscript>
    var checkValue = 1234567891231;        
    var done = "";
</cscript>
<cfloop from="11" to="1" index="i" step="-2">    
    <cfscript>
        check = check +   (ASC(MID(checkValue,#i#,1))-48)*3;
        check = check +   ASC(MID(checkValue,(#i#-1),1))-48;
    </cfscript>
</cfloop>
<cfset done = 10 - check/10>
<cfif done EQ 10><cfset done = 0></cfif>
<cfif done NEQ mid(checkValue,12,1)>
    <cfscript>
// error handler 
    </cfscript>
</cfif>

The values are passed in correctly but my ASC(MID()) is producing an error and I have not found out what I'm doing wrong.
Can someone give me a pointer?
Thanks!
EDIT: updated CF loop parameters
EDIT2: pinned it down to the 2nd mid-function getting 1-1=0 on the last loop iteration.

A:

I don't quite understand the algorithm here, but the error is because you're doing Mid(string,0,1) - which is one character before the start of the string, and thus doesn't work. (JS indexes strings from zero, CF indexes from one.)
Simple solution:
<cfloop from="11" to="1" index="i" step="-2">
    <cfset check += mid(checkValue,i,1) * 3 />
</cfloop>
<cfloop from="10" to="2" index="i" step="-2">
    <cfset check += mid(checkValue,i,1) />
</cfloop>

It doesn't waste time doing Asc since the values are already numbers (if they might not be, validate the input before this step), and also doesn't have unnecessary hashes.

