Is there a way to integrate an expression using Javascript? - javascript

I was creating a project that integrates and differentiates expressions by having like a calculator display for teh user to type an expression and it returns an differentiated/integrated expression.
The problem is that I am using the math.js library and the differentiation function math.derivative works but the integration function math.integral doesn't. Any help? I need the calculator to work for only symbolic integration/differentiation
I have tried linking javascript to other languages like python to do the maths and also I have tried different libraries but both didn't work
Can anyone just provide an simple example of integration using javascript that works and I will try to implement it in my code
Update: can anyone explain how to implement the mathjs-simple-integral library into the math.js libary
this is example code of what I tried doing after I installed the library using npm
import { integral } from 'mathjs-simple-integral';
var expression = "x^2";
var result = integral(expression, 'x').toString();
alert(result);

Related

Simple CLI calculator in js (least amount of code, using built-in functions only)

I'm trying to build the following calculator in every language and js raises questions.
This is my calculator in python3 that just works in 2 lines using the built-in eval() function:
#!/usr/bin/env python3
while True:
print(eval(input("Calculate: "))
My goal is to reproduce this calculator with the least amount of code and only using the built-in functions of the given language.
I'm a rookie in js and this is how far I've got:
#!/usr/bin/env node
while(true){
console.log(eval(prompt("Calculate: ")));
}
This is only working in theory and I understand that if I use Node as an interpreter I need to import prompt() and eval() functions to work but I'd like to avoid that because they aren't built-in and I thought I'd ask here first about the best approach.
Eval is already a native part of nodejs. Prompt, however, is not so simple, unfortunately. There is no trivial way to do this. I'd recommend to use a minimal library like prompt-sync instead.

python struct.pack equivalent for pure javascript

Unable find any specific pure javascript lib to implement below python syntax.
below is python syntax.
result = struct.pack('<i', 6).hex()
there are no pure js libraries which are similar to this feature but I have discovered one third party library python-struct which does the exact operations of python's struct have a look.
A sample from the website.
const struct = require('python-struct'); k=struct.pack(">f",49.9);
the out put will be a js buffer object

How to test small snippets of javascript code without a web browser?

I am trying to teach beginners javascript. Here is what I am trying to do:
I will give them a snippet of code such as a function with something incorrect:
const square = function(x) {
return x + x; // Wrong! This will double the number, not square them.
};
They will submit the code back to me.
I would like to write some test cases which would run on their code and evaluate if they are correct.
Does it make sense for them to submit their code as js files, and then I have a node.js project which reads their files and passes values to their functions and tests the response ?
Or does it make more sense to use a unit testing framework like mocha and chai etc ?
Both options would work, i.e Node directly or a test framework with test suites on their machines.
If you really want to get all their submissions and evaluate them on your own, you can simply ask them to export a function with a predefined name, in this case of this square exercise, it could be export function square { ..., then you add all those files to a folder, list all them using the fs module with something like fs.readdir/fs.readdirSync and dynamically call require on each file and execute the square function.
Note that this approach means you'll be running untrusted code on your machine, e.g one could potentially delete a file on your system (or actually do everything else possible with the privileges you execute the program). But you suggested that it's a beginner class and it looks like you know everyone, so that approach may be acceptable and in that manner, you don't have to send test cases for everyone and teach them how to run them (although it would be good at some point to do so).

How to make the javascript code easy to maintenance

All, I am working on a highly interactive web application which will need a lot of jquery or js code, And I'm finding that my code is becoming a little hard to maintain and is not all that readable. Sometimes even the author can't find the specified code.
So far what I had done for the clear code is below.
One js component in one js file .(for example. CustomTab.js is a tab component in my app.)
Using the templete to generate component HTML based on JSON.
Using Jquery UI.
Unobtrusive JavaScript.
Is there any other points I need pay attention? Anyway, Any suggestion or recommend technique for making js library/framework easy to miantanance is appeciated, thanks.
I could suggest you to use module pattern together with RequireJS to organize your JavaScript code. For the production you'll be able to use RequireJS optimizer to build your modules into one JavaScript file.
Also if you're expecting that your client-side application will be huge, consider to use some JavaScript MVC framework like Backbone.js together with the server-side RESTful service.
I use this namespacing pattern for my libraries:
MyLibrary.ListView.js:
var MyLibrary = MyLibrary || {};
MyLibrary.ListView = {
doSomethingOnListView: function() {
...
return this;
},
doSpecialThing: function() {
...
return this;
},
init: function() {
// Additional methods to run for all pages
this.doSomethingOnListView();
return this;
}
};
Whichever page needs this:
<script type="text/javascript" src="/js/MyLibrary.ListView.js"></script>
<script type="text/javascript">
$(function() {
MyLibrary.ListView
.init()
.doSpecialThing();
});
</script>
You can even chain methods if a certain page requires an additional function.
This is exactly the same question which I ask myself each time. I think there are few ways to get easy maintaining code.
Contribute in javascript opensource projects and understand how they solved that problem. I think you can gather some unique solution from each project and common part of projects structure will answer to your question about maintenance.
Use prepared solutions like backbone, knockout, ember or angularjs if I am not mistaken angular doesn't give you structure but provide you powerful tool for creating pages with less code. Also check todomvc for ready-made solutions.
Read books and try to create some structure for your needs. It will be difficult and long but result (maybe few years later :)) will be awesome.
Currently I'm also working on a JS framework for my company. What I'm doing is I use OOP elements for JS. In other words I'm implementing similar code to C# libraries(not that similar, simulating will be the correct word). As an example in C# you use Microsoft.Window.Forms, so I can use JSOOP and use method extending and overriding to create the same scenario. But if you gone to far in your project converting your JS code to JSOOP will be time consuming.
use JSLint, this will validate your code and bring down to a readable, script engine friendly code. Though JSLint is very strict so you can use JSHint also.
using seperate file for each component is a good idea I'm doing it also.
If you like you can download the jQuery developers version and you can have a general idea how they created the framework. I learned lot of thing looking at jQuery framework!

Context agnostic JavaScript Testing Framework

I'm looking for a JavaScript Testing Framework that I can easily use in whatever context, be it browser, console, XUL, etc.
Is there such a framework, or a way to easily retrofit an existing framework so its context agnostic?
Edit: The testing framework should not be tied to any other framework such as jQuery or Prototype.js and shouldn't depend on a DOM (or document object) being present. I'm looking for something to test pure JavaScript.
OK, here's something I just brewed based on some earlier work. I hope this would meet your needs.
jsUnity
Lightweight Universal JavaScript Testing Framework
jsUnity is a lightweight universal JavaScript testing framework that is
context-agnostic. It doesn't rely on
any browser capabilities and therefore
can be run inside HTML, ASP, WSH or
any other context that uses
JavaScript/JScript/ECMAScript.
Sample usage inside HTML
<pre>
<script type="text/javascript" src="../jsunity.js"></script>
<script type="text/javascript">
function sampleTestSuite() {
function setUp() {
jsUnity.log("set up");
}
function tearDown() {
jsUnity.log("tear down");
}
function testLessThan() {
assertTrue(1 < 2);
}
function testPi() {
assertEquals(Math.PI, 22 / 7);
}
}
// optionally wire the log function to write to the context
jsUnity.log = function (s) { document.write(s + "</br>"); };
var results = jsUnity.run(sampleTestSuite);
// if result is not false,
// access results.total, results.passed, results.failed
</script>
</pre>
The output of the above:
2 tests found
set up
tear down
[PASSED] testLessThan
set up
tear down
[FAILED] testPi: Actual value does not match what's expected: [expected] 3.141592653589793, [actual] 3.142857142857143
1 tests passed
1 tests failed
Jasmine looks interesting.
According to the developers, it was written because none of the other JS test frameworks met all their needs in a single offering and not requiring things like DOM, jQuery, or the window object is one of the explicit design points.
I'm thinking of using it with env.js and Rhino/SpiderMonkey/V8/etc. to write client-side tests for my web apps which can be easily run in all the same situations as Python unit tests. (setup.py test, BuildBot, etc.)
you might want to check out YUI Test. It should work fine without a DOM.
These run wherever javascript is enabled.
scriptaculous unit-testing
QUnit
There's also JSpec
JSpec is a extremely small, yet very powerful testing framework. Utilizing its own custom grammar and pre-processor, JSpec can operate in ways that no other JavaScript testing framework can. This includes many helpful shorthand literals, a very intuitive / readable syntax, as well as not polluting core object prototypes.
JSpec can also be run in a variety of ways, such as via the terminal with Rhino support, via browsers using the DOM or Console formatters, or finally by using the Ruby JavaScript testing framework which runs browsers in the background, reporting back to the terminal.
I just got Hudson CI to run JasmineBDD, at least for pure javascript unit testing.
Is JsUnit any help? It's designed to run in a browser, but it looks relatively abstract.

Categories

Resources