mathjs encountered an evaluation error: (intermediate value)(intermediate value)(intermediate value) does not function as expected

If I execute the following code snippet:


    obj = {};
    obj['number'] = 1;
    obj['expressionS'] = 'Sin(0.5 * c1)';
    obj['compiledExpressionS'] = null;

    let cnr = 'c' + obj.number;
    let params = {};
    params[cnr] = 2;

    var parsedExpressionS = math.parse(obj.expressionS);
    obj.compiledExpressionS = parsedExpressionS.compile();
    let value = obj.compiledExpressionS.eval(params);

An error is thrown at the last line which states: (intermediate value)(intermediate value)(intermediate value) is not a function. The code utilizes the Mathjs library (documentation available here: ). Despite my efforts, the reason behind this error remains unclear to me.

Answer №1

The problem arises from the fact that the value has not been compiled properly.

obj.compiledExpressionS = parsedExpressionS.compile(math);

In addition, ensure that Sin is written in lowercase.

Here is a working fiddle: http://jsfiddle.net/oxb3sp5o/414/

Working code example:

obj = {};
obj['number'] = 1;
obj['expressionS'] = 'sin(0.5 * c1)';
obj['compiledExpressionS'] = null;

let cnr = 'c' + obj.number;
let params = {};
params[cnr] = 2;

var parsedExpressionS = math.parse(obj.expressionS);
obj.compiledExpressionS = parsedExpressionS.compile(math);
let value = obj.compiledExpressionS.eval(params);
console.log(value);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Include an additional image with a JavaScript function

I recently started using a WordPress plugin known as User Submitted Posts. This plugin has the functionality to allow users to upload multiple images. I have configured it so that users can upload anywhere from 0 to a maximum of 3 images. However, when att ...

Reading a file in Javascript as it is being written: Techniques and methods

Let's consider a scenario where there's a log file, or in my situation, a binary data file. (I'm extracting data to create real-time stripcharts.) I want to read the entire file and perform operations on it, then have my FileReader trigger a ...

Guide on adding a username to the createUserWithEmailAndPassword authentication method in Firebase

I am currently working on implementing Username and Password Authentication in a web application built with react and firebase. However, being a beginner with firebase, I am having trouble finding detailed documentation on the process. I have come across F ...

What is the quickest method to identify duplicate entries within a JavaScript array?

let items = ['test0','test2','test0']; In the scenario shown above, we have two duplicate elements with the value "test0". What is the most efficient way to check for this duplication? ...

What are the best practices for establishing a secure SignalR client connection?

While tackling this issue may not be solely related to SignalR, it's more about approaching it in the most efficient way. In C#, creating a singleton of a shared object is achievable by making it static and utilizing a lock to prevent multiple threads ...

JavaScript Function to Redirect Page After a Delay of X Seconds

I'm trying to implement a redirect to a specific URL after displaying an error message for 5 seconds. Initially, I used JavaScript like this: document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000)); However, the redirectio ...

Traversing the nested arrays, processing column by column

Below is an array of subarrays const array = [ [0, 1, 2, 3], // Going from top to bottom? [4, 5, 6, 7, 8, 9], // | [10, 11, 12, 13, 14], // | [15, 16, 17, 18, 19], // | ] // V My goal is to achieve the fol ...

Transferring state information from a JSX navigation bar to a JSX map component in React

I'm having trouble figuring out how to utilize React in order to pass the state of my navigation bar to my map component. Essentially, I am fetching a date from the navigation bar and need to use it within my map component to display certain elements ...

Tips for securely concealing login details during an API call

As a newcomer to the world of Javascript and Vue.js, I am eager to expand my knowledge in these areas. However, I have encountered an issue while attempting to call an API login that exposes a password in the request payload. It seems quite insecure to ha ...

The client side encountered an issue when attempting to transfer file data to a nodejs server

In my current project, I am working on a simple file upload module using AngularJS (v1.3.4) with a Node.js server. For this, I decided to utilize the https://github.com/danialfarid/angular-file-upload library. While the library seems straightforward, I hav ...

Looking for a JavaScript function that can utilize AJAX to execute PHP code and display the PHP output on the webpage

I've been trying to use JavaScript to execute some PHP code and then display the results on the page. I came across a piece of code that achieves this using ajax and by placing the PHP code in an external file. However, the code I found seems to show ...

Construct a table featuring nested rows for every parent entry

Table 1 orderid customerName totalCost ---------------------------------- 1 Jonh £200.00 2 Ringo £50 Table 2 orderlineid orderid productName productPrice Quantity ----------------------------------------------- ...

The premature return of null in GraphQL mutation while integrating Stripe and MongoDB

I am facing an issue with my GraphQL mutation while trying to charge the user using Stripe. The payment is successfully processed on the server side and reflects in the database, but I am having trouble getting the updated user data back on the client side ...

When encountering an "uncaught SyntaxError" in the $.parseJSON function, one may want to investigate

When using this jQuery function, I encounter an Uncaught SyntaxError: Unexpected token u error if the cookie $.cookie('chk_ar') is undefined. function checkBgNoise() { // checks background noise option state, // activates 'on' click i ...

What is the best way to view or save the content of a PDF file using a web service?

As a newcomer to web services and JavaScript, I am facing a challenge with calling a web service that returns a PDF file in a specific format. Here is the link to view the PDF: https://i.stack.imgur.com/RlZM8.png To fetch the PDF, I am using the following ...

What is the best way to retrieve a previously used jQuery function?

The dynamic table I've created has the functionality to search, display search results in pagination, and auto-compute fields. This is how it works: Users search for their items in the search box If the search results are extensive, the system displ ...

Guide to creating and importing a JSON file in Wordpress

I'm looking to save a list in a JSON file within the Wordpress functions.php file. Can you guide me on how to achieve this? function ScheduleDeletion(){ ?> <script type="text/javascript"> var day = (new Date).get ...

What is the best way to retrieve the currently selected item from a Bootstrap filter/search feature?

I am currently working on implementing a filter and search feature using Bootstrap. The objective is that when the user searches for an item and the filter results in only one item remaining, if the user presses enter or "selects" the item, it should autom ...

Text input fields within a grid do not adjust to different screen sizes when placed within a tab

I noticed that my component under a tab is causing the Textfield to become unresponsive on small screens. To demonstrate this, I checked how the Textfield appears on an iPhone 5/SE screen size. https://i.stack.imgur.com/d8Bql.png Is there a way to make t ...

Unusual Behavior of JavaScript When Interacting with Bootstrap Sliders

I sought guidance from a web guru to enhance my code for creating a bootstrap slider per list item within a JavaScript for loop. However, the behavior of the slider has become inconsistent. At times it functions perfectly, while on other occasions it gene ...