1

I'm aware similar questions have been asked before, but my use-case is a little different.

I'd like to create an educational app similar to LeetCode, CodeWars, etc. where users can type in their own functions to solve algorithms.

Right now, I'm focusing on JavaScript, so the code could be evaluated on client-side.

I want to know, what considerations should I take into account to use eval() and new Function() safely?

Or is there a better alternative?

Bret Cameron
  • 451
  • 1
  • 4
  • 18
  • I would use something like [jupyter notebooks](https://jupyter.org/) before hacking my own framework. Specially for educational purposes. – Yan Foto Aug 06 '19 at 17:02
  • @YanFoto As far as you're aware, could I send the code to Jupyter Notebooks to be executed and get the result returned? Or would I need to use Jupyter's own IDE? – Bret Cameron Aug 06 '19 at 17:09
  • They also provide an online IDE. See https://jupyter.org/hub – Yan Foto Aug 07 '19 at 16:06

1 Answers1

3

Luckily you're not alone, and someone invented the wheel before you. You can check out NeilFraser/JS-Interpreter or sandbox.

There may be even better documented/implemented solutions that I'm not aware of, but it's a good start.

Gershon Papi
  • 5,008
  • 3
  • 24
  • 50
  • 1
    Thanks Gershon. I tried `js-interpreter` and ran into problems using ES6 (fat arrow syntax caused errors). But so far, `sandbox` looks like it does the trick - and I realise that CodeWars uses a variant of this. Thanks! – Bret Cameron Aug 06 '19 at 19:49