I have a function coded in vb. I am trying to convert it to Javascript
VB
Public Function Decrypt(ByVal s As String) As String
Dim Total As String
Dim Tmp As String
For i = 1 To Len(s)
Tmp = Mid(s, i, 1)
Tmp = Asc(Tmp) - 15
Tmp = Chr(Tmp)
Total = Total & Tmp
Next i
Decrypt = Total
End Function
So far this is what I have
function decrypt( s ) {
let tmp = ''
let total = ''
for ( let i = 1; i < s.length; i ) {
tmp = s.substring(i, 1)
tmp = tmp.charCodeAt(0) - 15
tmp = String.fromCharCode(tmp)
total = total tmp
}
return total
}
Im getting the error RangeError: Invalid string length
at line total = total tmp
How can I fix this function please
CodePudding user response:
This is exact translation. Tested in excel VBA.
function decrypt(s) {
var total = ""
for (var i = 1; i <= s.length; i ) {
var tmp = s.substring(i - 1, i);
total = String.fromCharCode(tmp.charCodeAt(0) - 15);
}
return total
}
console.log(decrypt("hello"))