Hi,
I managed to create a nice set of template to work in Javascript with Coderunner, using prompt and a fixed random sequence. I shared my prototypes in another thread.
I am now trying to make the template seed the random number differently per test.
This is the starting code:
const randomValues = [ 0.15, 0.23, 0.52, 0.34, 0.75, 0.38, 0.62, 0.01, 0.29, 0.40, 0.92, 0.54, 0.42, 0.09, 0.10, 0.07, 0.88, 0.46, 0.01, 0.21, 0.17, 0.64, 0.06, 0.57, 0.25, 0.29, 0.81, 0.57, 0.02, 0.62,0.03, 0.38, 0.64, 0.75, 0.22, 0.49, 0.89, 0.92, 0.13, 0.72 ];
let randomIndex = 0;
Math.random = () => randomValues[randomIndex++ % randomValues.length];
Doing it this way, each test starts with the first element of the array (0.15). I would like for the template to give different starting index for every test case. In my mind a great solution is to use the test number. So i did:
const randomValues = [ 0.15, 0.23, 0.52, 0.34, 0.75, 0.38, 0.62, 0.01, 0.29, 0.40, 0.92, 0.54, 0.42, 0.09, 0.10, 0.07, 0.88, 0.46, 0.01, 0.21, 0.17, 0.64, 0.06, 0.57, 0.25, 0.29, 0.81, 0.57, 0.02, 0.62,0.03, 0.38, 0.64, 0.75, 0.22, 0.49, 0.89, 0.92, 0.13, 0.72 ];This works, but only while I'm editing the question. The code correctly evaluates to the test number.
let randomIndex = {{ TEST.rownum }};
Math.random = () => randomValues[randomIndex++ % randomValues.length];
If I try to test it in preview mode, it stops working because {{ TEST.rownum }} stop existing and evaluates to nothing!
In TWIG that line evaluates to let randomIndex = ;
I tried some debugging, I printed the TEST object as a JSON and, just as I thought, the rownum field does not exist!
What am I doing wrong?
This is my complete template for reference. Starting from nodejs, with is_combinator disabled:
const inputs = `{{ TEST.stdin }}`.split('\n') let inputIndex = 0; global.prompt = () => inputs[inputIndex++]
const randomValues = [ 0.15, 0.23, 0.52, 0.34, 0.75, 0.38, 0.62, 0.01, 0.29, 0.40, 0.92, 0.54, 0.42, 0.09, 0.10, 0.07, 0.88, 0.46, 0.01, 0.21, 0.17, 0.64, 0.06, 0.57, 0.25, 0.29, 0.81, 0.57, 0.02, 0.62,0.03, 0.38, 0.64, 0.75, 0.22, 0.49, 0.89, 0.92, 0.13, 0.72 ];
let randomIndex = 0;
Math.random = () => randomValues[randomIndex++ % randomValues.length];
{{ STUDENT_ANSWER }}
var SEPARATOR = "#<ab@17943918#@>#";
{{ TEST.testcode }} https://coderunner.org.nz/pluginfile.php/1564/mod_forum/post/3073/PROTOTYPE_JavascriptWithPromptAndRandom.xml