javascript - window.prompt alternative

javascript - window.prompt alternative

by Rui Figueiredo -
Number of replies: 13

Hi!


I'm trying to do a javascript question in coderunner that ask the user a value and calculate his double:

num = window.prompt("Number:");

d = num * 2;

console.log(d);

Is there a simple way to change the first instruction to get the input in Coderunner using nodejs?


Thank you in advance.

Figueiredo


In reply to Rui Figueiredo

Re: javascript - window.prompt alternative

by Richard Lobb -

I haven't used nodejs in CodeRunner for several years. But will the readline module do what you want?

If you set the template (a non-combinator one) to just {{ STUDENT_ANSWER }} the following code reads a number from standard input and prints it doubled:

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('Number: ',(n) => { console.log(2 * n); });

Image of question

I should mention I've never used this module before, so you'll have to explore its suitability yourself.

BTW: while entering the above I noticed there was an error in the nodejs question type template: it's a combinator template and uses print instead of console.log to output the separator between tests. It turns out, however, that nodejs questions still work, albeit more slowly, because the run time error resulting from the wrong function causes CodeRunner to drop back to a per-test mode, which doesn't require a separator. That's why my standard tests didn't pick the error. I'll fix it in the next release.

In reply to Richard Lobb

Re: javascript - window.prompt alternative

by Rui Figueiredo -

Thank you so much Richard Loob for your quick response.

I was using coderunner in an introductory C # programming course, and decided to use JavaScript instead.

As it is very difficult for newbies to understand the concepts behind the setup needed to get everything working, I will continue with C #.

Thanks again and congratulations for your amazing CodeRunner.

Rui Figueiredo

In reply to Richard Lobb

Re: javascript - window.prompt alternative

by Rui Figueiredo -

Hi Richard.


I'm trying again with the readline-sync module. So I ask you how can I install this module in my jobe server and will be recognized by codeunner?


Thank you again.

In reply to Rui Figueiredo

Re: javascript - window.prompt alternative

by Richard Lobb -

I think the following 2 commands will do it. 

sudo apt install npm
sudo npm install readline-sync -g

In reply to Richard Lobb

Re: javascript - window.prompt alternative

by Rui Figueiredo -
Hi Richard!

I did those commands, and now I have those modules installed globally:

/usr/local/lib

+-- readline-sync@1.4.10

+-- sass@1.22.10

`-- typescript@3.6.2

But when I tried to submit this code:


var readlineSync = require('readline-sync');

 

// Wait for user's response.

var userName = readlineSync.question('May I have your name? ');

console.log('Hi ' + userName + '!');


Jobe server throw this error:

***Error***

module.js:549

    throw err;

    ^


Error: Cannot find module 'readline-sync'

    at Function.Module._resolveFilename (module.js:547:15)

    at Function.Module._load (module.js:474:25)

    at Module.require (module.js:596:17)

    at require (internal/module.js:11:18)

    at Object.<anonymous> (__tester__.nodejs:1:82)

    at Module._compile (module.js:652:30)

    at Object.Module._extensions..js (module.js:663:10)

    at Module.load (module.js:565:32)

    at tryModuleLoad (module.js:505:12)

    at Function.Module._load (module.js:497:3)


Have you some clue for what is happening?


Thx.

In reply to Rui Figueiredo

Re: javascript - window.prompt alternative

by Rui Figueiredo -

Hi Richard!


I run the command "npm install readline-sync" at /home/jobe/runs. And I tried a question that had this result:

readline-sync error


Now I'm blocked again...

In reply to Rui Figueiredo

Re: javascript - window.prompt alternative

by Richard Lobb -

Have you set up a readline interface to take input from process.stdin as in the example I showed above? Please try running exactly that example.

In reply to Richard Lobb

Re: javascript - window.prompt alternative

by Rui Figueiredo -

Yes, I did and it worked. 

The problem is that I wanted a sync readline input because my course is a introductory to programming for multimedia students that are integrated in anothers couse in the following order: first they will learn html+css, then learn coding with javascript and finally PHP+SQL databases.

In others courses I used C# with coderunner to introduce coding and it was great.  So I'd like to replicate that course to Javascript. But the readline method uses an asynchronously way that is to complex to explain to newbies.

So I tried the readline-sync package but that throw the error I mentioned above.

The problem now is that the jobe server tell that stty is not set:


stty: when specifying an output style, modes may not be set

But when I run a script in the "runs" folder exactly the same, it works!


There is some chance to set the stty mode in jobe server?




In reply to Richard Lobb

Re: javascript - window.prompt alternative

by Rui Figueiredo -

Yes, I did and it worked. 

The problem is that I wanted a sync readline input because my course is a introductory to programming for multimedia students that are integrated in anothers couse in the following order: first they will learn html+css, then learn coding with javascript and finally PHP+SQL databases.

In others courses I used C# with coderunner to introduce coding and it was great.  So I'd like to replicate that course to Javascript. But the readline method uses an asynchronously way that is to complex to explain to newbies.

So I tried the readline-sync package but that throw the error I mentioned above.

The problem now is that the jobe server tell that stty is not set:


stty: when specifying an output style, modes may not be set

But when I run a script in the "runs" folder exactly the same, it works!


There is some chance to set the stty mode in jobe server?




In reply to Richard Lobb

Re: javascript - window.prompt alternative

by Rui Figueiredo -

Hi Richard!


I put everything to work like I inted with a tip form anseki/readline-sync: instead of using readline-sync I used syncprompt package. I'm testing and at the moment it is working.

var prompt = require('syncprompt');

 var answer = prompt('');

console.log('\nHi ' + answer+'!');


Thank you very much for your help.


In reply to Rui Figueiredo

Re: javascript - window.prompt alternative

by Richard Lobb -

Good to know you've found a solution. Well done.

In reply to Rui Figueiredo

Re: javascript - window.prompt alternative

by Jack Steel -

I know you solved this another way, but there is a way to make your example code work if you wanted to use that/for future reference.

You'd need to override the window.prompt to return the test value.

Thus should be a working as a customized nodejs question with below as the template:


const window = {};

window.prompt = _ => {{ TEST.stdin }};

{{ STUDENT_ANSWER }}


Make sure it's not a combinator question and then the test cases should provide the input in the standard in.

Note we need to create the window object as it doesn't exist in the normal runtime, then we set the window.prompt function to take some ignored input and just return the TEST.stdin variable.

The only thing you'd need to change in your example code is using var/const to specify the variables:


const num = window.prompt("Number:");

console.log(num * 2);


See attached for an exported solution that's working as expected.

In reply to Jack Steel

Re: javascript - window.prompt alternative

by Rui Figueiredo -
Thank you Jack.

I will try  to test your way.

Bye