Online math editor and evaluation engine - javascript

I'm trying to make a website that would help my students to evaluate their math knowledge.
The subject involves simple algebra like 5a-(2a+(2b-3a))= ? and simple fractions.
I have two problems:
Students have to be able to input math easily. They have no experience with code like TeX. I can't expect them to input stuf like \frac{a}{b}.
Which easy to use math editor library would you recommend?
How can i evaluate their answers. How can i evaluate input like 5a+2ab = 2ab+5a. I already tried something out but student said afterwards that they entered 5 a of 5*a instead of 5a and the system said it was wrong...
Are there any (javascript) library's that could help me accomplish this?
Thanks!

WolframAlpha has an API. It can handle such requests fairly easily: Demo
It has a very comprehensive parsing engine that'll understand most correctly formatted mathematical expressions. Although I must admit that it might be a bit overkill...

Related

JavaScript BigInt with a gmp-style API, specifically mpfr

I am writing a transpiler from my desktop programming language to JavaScript.
I use gmp on the desktop, so am writing a thin wrapper to mimic the same entry points but use BigInt under the hood.
(NB Emscripten etc NOT involved) So far mpz and mpq are working pretty well, ~30 entry points each, done by hand, so now I'm wondering about mpfr.
Could mpfr be done as mpq with implied/capped denominator of 10^k (where k can be negative), and
accordingly truncated/BigInt numerator? I expect a bit of a struggle with mpfr_const_pi(), mpfr_sin/log/exp(), etc. I say 10^k but am not even certain of that vs 2^k.
I have studied https://github.com/MikeMcl/big.js and friends but no offence meant all that seems to pre-date BigInts, and I simply cannot find anything that implements floats via BigInt.
In short, what code needs to be in mpfr.js so that the following will work (ideally unaltered), obviously any partial ideas, hints, or tips are just as welcome as a full-blown working example. You can assume (eg) mpz_get_str() is available, or of course you can go with using (say) BigInt.toString() etc directly, and not overly panic about precisely where the decimal point has to go, or any "%.75Rf" related nuances. I just need something to get the ball rolling.
<script src="mpfr.js"></script>
<script>
mpfr_set_default_prec(252); // (enough for 75 decimal places)
let one_third = mpfr_init(1); // (ok, non-std syntax, anyway init to 1)
mpfr_div_si(one_third,one_third,3);
console.log(mpfr_sprintf("%.75Rf",one_third);
</script>
I finally found this https://jrsinclair.com/articles/2020/sick-of-the-jokes-write-your-own-arbitrary-precision-javascript-math-library/ and I've now got pretty much everything I needed working.
While it is exactly what I was looking for, I should point out that it is deeply flawed, for instance there is a frankly outrageous memoize() function liberally applied, which no doubt vastly improved some pointless benchmark but would totally cripple real-world use, and other gross ineffiencies such as exp10(n) returns BigInt(1${[...new Array(n)].map(() => 0).join("")}), instead of the much saner
10n**BigInt(n). Nevertheless it is quite spirited and undeniably well meant, with plenty of good ideas.
Should anyone wish to see the results of my efforts I have uploaded the latest version: https://github.com/petelomax/Phix/blob/master/pwa/builtins/mpfr.js

How do I identify application identifiers and their values from a GS1 2D Barcode (2D Datamatrix)?

I am attempting to identify values from a 2D barcode for parsing within a piece of software.
Essentially, when scanning 2D barcodes, I obtain an incomprehensible string such as:
011234567890123410abcde21fghijk17200101
I have managed to program a couple of scanners to 'identify' the application identifiers by encasing them in parentheses such as:
(01)12345678901234(10)abcde(21)fghijk(17)200101
This requires a configuration change to the scanners, but I'd like my software rather than my hardware to identify the values, rather than relying on proprietary scanners to do the work for me.
I have got as far as lifting out Group Separators from variable length fields, then I have seem to have come stuck.
Any and all help appreciated, I have been racking my brain for days. Our languages of choice are C# & Javascript, but if anybody has the logic, even if it's pseudocode, that would be more than helpful.
For your problem, this document GS1 DataMatrix Guideline distributed by GS 1 will be of help.
Directly there is an answer in the flow chart on page 18.
However, you will need to read other parts as well.
In any case, deleting something like a group separator makes it impossible to solve the problem.
Please obtain and retain all the information and then process it.

ES8 - Why have padStart/padEnd methods?

Trying to understand the reasoning behind the support for these 2 methods in ES8. padEnd for example - this can be achieved either using concat, replace, repeat.
So is it just to have a cleaner way of achieving this because this could be a common use-case or this is more efficient than current alternatives?
Edit: It would help to know why a question is down voted - was the question too opinionated/broad to ask?
It's just for convenience. There are a huge amount of functions that could be done using other low level means - but when written poorly they result in bugs, or inefficient code. Everyone wins when the language adds support for something people often do.
To exaggerate your example - languages don't need for loops either. You can generally write the same sort of code with a while loop. People don't need ternaries - they can be done with a standard if statement. In both of the examples people would generally need to write more code to achieve the same effect - but why make the coder do that?
I would reverse the question - why do you think they shouldn't include padEnd?
I think your question is asking for usecases of padstart, padend functions, i.e what prompted for these to be included in ecmascript.
As pointed above, they are helper functions that let you achieve
more with less code.
Displaying tabular data in a monospaced font.
Adding a count or an ID to a file name or a URL: 'file 001.txt'.
Aligning console output: 'Test 001: ✓'.
Printing hexadecimal or binary numbers that have a fixed number of digits: '0x00FF'
You can read more about its usecase/applications here :
http://exploringjs.com/es2016-es2017/ch_string-padding.html
https://www.theregister.co.uk/2017/07/12/javascript_spec_straps_on_padding/

Collect prices from page using JavaScript

I'm developing a bookmarklet now and faced this task: I need to collect all prices from any page.
The problem is that the price may be in multiple formats ($19.00, 15.45$, etc), not counting different currencies and html markup. The good news is that I'm using jquery.
If anybody has an idea how it can be accomplish, please share :)
Thanks in advance!
If there is no consistent markup you're probably going to have to write some regex's for the known patterns. For example:
To capture a pattern like $19.00 you'll use a regex that looks something like this:
\$[0-9]*.?[0-9]{1,2}
Since you're target data is so unstructured i'm not sure there is a single good answer to this. You'll need to identify the patterns you are looking for and write the regex's to identify them.
Test your regular expressions here: http://regexpal.com/
Best of luck.
-R

Are there any javascript frameworks for parsing/auto-completing a domain specific language?

I have a grammar for a domain specific language, and I need to create a javascript code editor for that language. Are there any tools that would allow me to generate
a) a javascript incremental parser
b) a javascript auto-complete / auto-suggest engine?
Thanks!
An Example of implementing content assist (auto-complete)
using Chevrotain Javascript Parsing DSL:
https://github.com/SAP/chevrotain/tree/master/examples/parser/content_assist
Chevrotain was designed specifically to build parsers used (as part of) language services tools in Editors/IDEs.
Some of the relevant features are:
Automatic Error Recovery / Fault tolerance because editors and IDEs need to be able to handle 'mostly valid' inputs.
Every Grammar rule may be used as the starting rule as an Editor/IDE may only want to implement incremental parsing for performance reasons.
You may want jison, a js parser generator. In terms of auto-complete / auto-suggest...most of the stuff out there I know if more based on word completion rather than code completion. But once you have a parser running I don't think that part is too difficult..
This is difficult. I'm doing the same sort of thing myself.
One approach is:
You need is a parser which will give you an array of the currently possible ASTs for the text up until the token before the current cursor position.
From there you can see the next token can be of a number of types (usually just one), and do the completion, based on the partial text.
If I ever get my incremental parser working, I'll send a link.
Good luck, and let me know if you find a package which does this.
Chris.

Categories