I'm trying to run Javascript through the BE_EvaluateJavascript function in FM18 to produce a registration code.
My code cues the error 'SyntaxError: unterminated statement (line 10)'.
I applied the following Substitute suggestion from another post to fix the issue:
Let([
~input = JavaScript_Code_original ;
_BS = "\" ;
_SQ = "'" // Single quote
];
Substitute( ~input ;
[ _BS ; _BS & _BS ];
[ Char( 10 ); _BS & "n" ];
[ Char( 13 ); _BS & "r" ];
[ Char( 9 ); _BS & "t" ];
[ _SQ ; _BS & _SQ ]
)
)
This produces the error 'SyntaxError: invalid escape (line 1)' (because the Javascript has been turned into a single screed...?).
Here is the Javascript I am trying to process:
const solution_key = '" & $solution_key & "';
const reg_name = '" & $username & "';
const users = " & $users & ";
function generate_registration_code(reg_name, users) {
if (!reg_name || !solution_key) return false;
if (users > 0) reg_name += users;
const len = reg_name.length;
let chr = 0, pos = 0;
for (let loop = 0; loop < len; loop++) {
const ascii = reg_name.charCodeAt(loop);
chr += ascii;
pos += ascii * (loop + 1);
}
const sum = chr + pos + len;
const num_string = sum.toString() + pos.toString() + chr.toString() + len.toString() + (sum * sum).toString();
let code_pre_formatted = '', loop = 0;
while (loop < num_string.length) {
const segment = parseInt(num_string.substr(loop, 2), 10);
const convert_num = segment > 56 ? parseInt(num_string.charAt(loop), 10) : segment;
if (convert_num === 0) {
code_pre_formatted += solution_key.charAt(0) + solution_key.charAt(1);
} else {
code_pre_formatted += solution_key.charAt(convert_num);
}
loop += convert_num.toString().length;
}
let code_formatted = '';
for (let i = 0; i < code_pre_formatted.length; i += 4) {
code_formatted += (i > 0 ? '-' : '') + code_pre_formatted.substr(i, 4);
}
if (users > 0) code_formatted += '-[' + users + ']';
return code_formatted;
}
generate_registration_code(reg_name, users);
Regards,
Morley