This question already has answers here:
Evaluating a string as a mathematical expression in JavaScript
(26 answers)
Closed 8 years ago.
I am trying to create a simple graphing calculator where a user enters a function of f (like f(x) = x^2+2x+6). Basically the javascript replaces the x in the function with some number and then evaluates the function using eval(). The problem is, I want users to be able to type x^2 instead of default javascript which would be Math.pow(x,2). I'm guessing it's going to be some regular expression but I have little experience with them and find them really confusing, personally. Is it possible to convert a statement like x^3-x^2 to Math.pow(x,3)-Math.pow(x,2) ??
Help greatly appreciated.
You want to use a Regular Expression that looks something along the lines of
(.+)\^(.+)
This will match both selections, you then replace the instances of that string, using those matches like this.
Math.pow($1, $2)
Javascript has support for this kind of operation with the function option in String.prototype.replace
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter
1) Yes, it is possible. You can easily program it yourself if you parse what the user entered. Wherever you see x^n just turn it into Math.pow(x,n). And only then eval.
This will work for say polynomials of one variable.
2) If you want to solve this more generally (for a broader class of math functions),
you need to come up with some grammar and build an AST from the user input.
http://en.wikipedia.org/wiki/Abstract_syntax_tree
Related
This question already has answers here:
JavaScript -- write a function that can solve a math expression (without eval)
(2 answers)
Closed 3 years ago.
I'm working on a problem generator that spits out phrases like "!(A && B) || (A || B)", but it does so in a string (I have a function that spits this out in string form). How would I convert this string expression into a same boolean expression in javascript? I've tried to use JSON.parse() but it keeps giving me an error.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval should be the function you're looking for, however read the note about the huge security risk!
This question already has answers here:
Javascript parse float is ignoring the decimals after my comma
(10 answers)
Closed 7 years ago.
Ok so I'm having a bit of trouble returning a number from an html tag using jquery.
So lets say i have this: <p class="number">4,500.50</p> and i want to get the number from this tag using jquery, so I have the following.
var number = parseFloat($('.number').html());
But this only returns the number 4 instead of the full number. I also treied with the .text() method but the result is the same. Any ideas as to how to resolve this? Any help is appreciated.
Example jsfiddle: https://jsfiddle.net/zf1ctums/1/
Your input string has a non standard (well probably in certain contries people are used to it) format. parseFloat only knows about digits and decimal POINT. So you need to delete the commas:
var number = parseFloat($('.number').html().replace(',', ''));
The problem is that the number should be 4500,50.
You should not pass the thousand separator.
Take a look here.
This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 9 years ago.
I was just trawling through the QUnit source code (1.12.0) and came across a line that confused me. I've done a bit of googling and haven't been able to come up with a reason for it.
Source: http://code.jquery.com/qunit/qunit-1.12.0.js line 520
result = !!result;
A similar thing appears further on in the code, except instead of storing the result in itself, it's storing the double negated variable in JSON.
Source: http://code.jquery.com/qunit/qunit-1.12.0.js line 957
result: !!result
As ! negates, I assume !! will negate then negate again, thus ending up with exactly what you started with. In which case, what is achieved by setting a variable equal to itself, negated twice? (Or in the latter example, returning itself negated twice instead of just returning itself.)
!! is used to convert the value to the right of it to its equivalent boolean value.
Also check this related Thread.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I access a property of an integer with a single dot?
I was reading an article, and came across the strange behaviour of javascript toFixed method. I don't understand the reason for the last statement. Can anyone explain please?
(42).toFixed(2); // "42.00" Okay
42.toFixed(2); // SyntaxError: identifier starts immediately after numeric literal
42..toFixed(2); // "42.00" This really seems strange
A number in JavaScript is basically this in regex:
[+-]?[0-9]*(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?
Note that the quantifiers are greedy. This means when it sees:
42.toFixed(2);
It reads the 42. as the number and then is immediately confronted with toFixed and doesn't know what to do with it.
In the case of 42..toFixed(2), the number is 42. but not 42.. because the regex only allows one dot. Then it sees the . which can only be a call to a member, which is toFixed. Everything works fine.
As far as readability goes, (42).toFixed(2) is far clearer as to its intention.
The dot is ambiguous: decimal point or call member operator. Therefore the error.
42..toFixed(2); is equivalent to (42.).toFixed(2)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A comprehensive regex for phone number validation
Validate phone number with JavaScript
I'm trying to write a regular expression to validate US phone number of format
(123)123-1234 -- true
123-123-1234 -- true
every thing else in not valid.
I came up something like
^\(?([0-9]{3}\)?[-]([0-9]{3})[-]([0-9]{4})$
But this validates,
123)-123-1234
(123-123-1234
which is NOT RIGHT.
The easiest way to match both
^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$
and
^[0-9]{3}-[0-9]{3}-[0-9]{4}$
is to use alternation ((...|...)): specify them as two mostly-separate options:
^(\([0-9]{3}\)|[0-9]{3}-)[0-9]{3}-[0-9]{4}$
By the way, when Americans put the area code in parentheses, we actually put a space after that; for example, I'd write (123) 123-1234, not (123)123-1234. So you might want to write:
^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$
(Though it's probably best to explicitly demonstrate the format that you expect phone numbers to be in.)