6

I am trying to teach beginners javascript. Here is what I am trying to do:

I will give them a snippet of code such as a function with something incorrect:

const square = function(x) {
   return x + x; // Wrong! This will double the number, not square them.
}; 

They will submit the code back to me. I would like to write some test cases which would run on their code and evaluate if they are correct.

Does it make sense for them to submit their code as js files, and then I have a node.js project which reads their files and passes values to their functions and tests the response ?

Or does it make more sense to use a unit testing framework like mocha and chai etc ?

1
  • 1
    If you're interested - I took the approach of getting people to use node and run some jest tests. It has the added advantage of getting people used to git and command line github.com/dwjohnston/javascript-coding-challenges Commented Oct 4, 2019 at 8:09

1 Answer 1

1

Both options would work, i.e Node directly or a test framework with test suites on their machines.

If you really want to get all their submissions and evaluate them on your own, you can simply ask them to export a function with a predefined name, in this case of this square exercise, it could be export function square { ..., then you add all those files to a folder, list all them using the fs module with something like fs.readdir/fs.readdirSync and dynamically call require on each file and execute the square function.

Note that this approach means you'll be running untrusted code on your machine, e.g one could potentially delete a file on your system (or actually do everything else possible with the privileges you execute the program). But you suggested that it's a beginner class and it looks like you know everyone, so that approach may be acceptable and in that manner, you don't have to send test cases for everyone and teach them how to run them (although it would be good at some point to do so).

Sign up to request clarification or add additional context in comments.

3 Comments

To make it safer, is there a way to evaluate their code within a vm like javascriptcore ?
Take a look at Node's vm module, also npmjs.com/package/vm2 which is an extended user version of it.
Also, take a look at something like jest with a watcher. It is simple to set up and you can just run the tests via your terminal/console. Give them a .js file which contains the instructions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.