How to make a function that will calculate the area under the curve of the graph using node.js?

I need to make the function so that it calculate the area when I enter function f and coefficients a, b and n on the command line.

For now I have this:

module.exports = function (f,a,b,n,next) {

parseFloat(a);
parseFloat(b);
parseInt(n);
var h = (b-a)/n;
var s = 0;
var suma = function () {
    for (var i = 0; i <= n; i++) {
        var ximj = a+h*(i-1);
        var xi = a+h*i;
        var x = (ximj + xi)/2;
        s += eval(f,x);
    }
    return s;
};

if (n<1) {
    next(new Error("Koeficijent n je broj ekvidistantnih točaka."))
}
else next(null, {povrsina : function () {return h*suma();}}
    );

I think that the function suma() isn’t working like it should be working.

In command line this should be look like :

f: x*x-2*x+7
a: 2
b: 4
n: 10
Povrsina ispod grafa funkcije f je 20.66000...

My prompt file looks like this:

var pov = require('./integrator.js');

var prompt = require('prompt');

prompt.get (['f','a','b','n'],function (err,koef) {
if (err) return console.log(err);
console.log("f: ",koef.f);
console.log("a: ",koef.a);
console.log("b: ",koef.b);
console.log("n: ",koef.n);

parseFloat(koef.a);
parseFloat(koef.b);
parseInt(koef.n);

pov(koef.f,koef.a,koef.b,koef.n,function(err,rj){
    if (err)
        console.log(err);
    else 
        console.log("Povrsina ispod grafa funkcije f je " + rj.povrsina());
});

Thank you for help 🙂

1 thought on “How to make a function that will calculate the area under the curve of the graph using node.js?”