There's a simple Articles library available, among other things, which lets you easily do this (for English). You could try using it as a base, or an idea to adapt to your needs, or just straight-up use the library itself - but then you need to put it in a wrapper for it to work, since it's written for npm/NodeJS only.
Alternatively, I created a minified version for SugarCube (works as a module with requireJS too if you use it), just copy & paste it in your JavaScript, then use ...
<<= setup.Articles.articlize($variable)>>
If you want to know just the article, you can also use ...
<<set _article = setup.Articles.find($variable)>>
... which will set _article to "a" or "an" depending on the contents of $variable.