bent0b0x / math-expression-generator

JavaScript library for generating mathematical expressions that evaluate to a particular number
3 stars 1 forks source link

I can't make it work #46

Open Sonotak opened 1 year ago

Sonotak commented 1 year ago

UPD: Solved, apologies for an emotional wall of text.

Sorry, I've just needed this program and I do not know anything about JavaScript. I've installed node.js and math-expression-generator via npm i math-expression-generator but then I got really stuck. I've created a .js file with the code: import { generateExpression } from "math-expression-generator";

const expression = generateExpression({ target: 20, length: 2 })

The return I've got was "Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension."

So I've renamed the file to .mjs as I do not know how to " set "type": "module" " with the same code. The new result:

"SyntaxError: Named export 'generateExpression' not found. The requested module 'math-expression-generator' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using:

import pkg from 'math-expression-generator'; const { generateExpression } = pkg;"

So I've tried to change the code (which is an unknown field to me) to:

import pkg from 'math-expression-generator'; const { generateExpression } = pkg;

const expression = generateExpression({ target: 20, length: 2 });

The result was empty. Then I've tried:

import pkg from 'math-expression-generator'; const { generateExpression } = pkg;

const expression = pkg({ target: 20, length: 2 });

The result was:

TypeError: pkg is not a function

I've tried this in Windows' cmd and in Visual Studio Code. I've also tried several online JS code runners, but none worked with modules it seems.

After about an hour of experimenting I give up and feel like an idiot, I probably am as I've had little idea of what I've been doing and now I write this noobish text which 99% would never be answered as usually happens. Well, anyway - I am completely stunned and don't know how to make this program work, so a more detailed guide would've been wonderful. It is really sad that many very useful programs do not have friendly access routes for non-coders. I've had in the past to get a grasp of Ruby and then, separately, of TEX, and also of Python to make certain programs which I've needed to work which, in my personal opinion, is absolutely not user-friendly. In the above cases I've managed to make the programs run, at least, but not in this one - it is still fine and very welcomed to create "raw" programs, but if they'd run, of course. I am sorry for being a complete lamer, but I really do not understand how to make this program work.


Solved. The code above worked:

import pkg from 'math-expression-generator'; const { generateExpression } = pkg;

const expression = generateExpression({ target: 20, length: 2 });

It just doesn't have a command to print out the result. The fully working code would be:

import pkg from 'math-expression-generator'; const { generateExpression } = pkg; const expression = generateExpression({ target: 20, length: 2 }) console.log(expression);

Sonotak commented 1 year ago

After trying it out, I have to agree with dyc3's feedback: it tends to use zeroes and many expressions are incorrect. For length 40 more than half of expressions are incorrect and 80% of numbers in an expression are always zeroes. Hopefully this program could be very significantly improved to make it useful.