At my school, we are using JavaScript as an introductory programming language, utilizing prompt
for input and console.log
for output. However, CodeRunner supports Node.js, which does not include the prompt
function.
Through various workarounds, I have managed to:
- Redirect input from the Standard Input field to work with
prompt
. - Override
Math.random()
to always produce the same numbers. - Create a gap-filler question type for JavaScript.
I am sharing these solutions in case they might be useful for others in the future. If the developers find them relevant, they could also be added to the references to assist those searching for similar solutions.
1- Redirect input from the Standard Input field to work with prompt
.
Add in the customisation field, before {{ STUDENT_ANSWER }}, the following code:const inputs = `{{ TEST.stdin }}`.split('\n'); let inputIndex = 0; global.prompt = () => inputs[inputIndex++];
2- Override Math.random()
to always produce the same numbers.
Add in the customisation field, before {{ STUDENT_ANSWER }}, the following 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];
You can download my XML with the prototype with both prompt and random workaround at this link: https://coderunner.org.nz/pluginfile.php/1564/mod_forum/post/3073/PROTOTYPE_JavascriptWithPromptAndRandom.xml
3- Create a gap-filler question type for JavaScript.
As I was translating the question prototype to English I realized there is still a bug I need to fix. I'll come back to this later with the prototype.If somebody needs anything, feel free to ask.
Bye!