for example, if I had a STRING = "3+3*(4-1)"
if a ran a function and wanted it to solve the equation using javascript, what Would I start with?
Assuming the equation has a correct syntax of javascript, you can use eval to evaluate the value.
i.e.
eval("3+3*(4-1)") = 12
Related
I'm trying to make a calculator with JS. Is there a way to convert a string like var calculation = "11+34*6" into a "number" so JS can print out the solution?
Use eval() function to solve your calculation even it is a string.
let calculation = '11+34*6';
let result = eval(calculation);
document.write(result);
For better understanding read the documentation on Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
You can use JavaScript eval function, that allows you to evaluate or execute a string argument, e.g.
console.log(eval("11 + 34 * 6"));
I have a java script function that I'm trying to replicate in python 2, and the java script is doing some kind of precision error wrap around (or something) which I'm having trouble understanding. These are large numbers, but here's the example:
In javascript:
a = 3141592751
b = 1234567890
result = (a*31) ^ b
window.alert(result)
Here, result = -447877661. I'm assuming it's because of a bit limitation on storing large numbers and the related wrap around to a large negative number.
Using python 2:
a = 3141592751
b = 1234567890
result = (a*31) ^ b
print result
Here, result = 98336370147, which is correct mathematically.
How can I replicate the functionality of the javascript code using python? What is the wrap around point? Thanks!
The limit of a variable in javascript is-
+/- 9007199254740991
i.e., 2^53 -1
One more thing to consider is that if you are dealing with bitwise operators and shift operators to get your job done, they operate on 32-bit ints, so in that case, the max safe integer is-
2^31-1, or 2147483647
Hope it helps!
More reference - MDN and StackOverflow
So, you may have to use this value to wrap your python code.
Note : ^ symbol above represents power of.
I have a script assigns a variable using parseFloat as follows:
var vendorCost = parseFloat(vendorSearchresults[0].getValue('vendorcost')).toFixed(2);
I assumed this would make the variable a number. However when I check it with typeof - it reports it as a string. My solution is as follows:
vendorCost = parseFloat(vendorCost);
Which works, however I'm trying to be more efficient when coding and would like to understand why it doesn't make vendorCost a number when assigning it a number? Is there a way I could make the first statement make vendorCost a number without the need for the second statement? Thanks in advance.
Update - just thought I should mention I'm having the exact same issue without using .toFixed -
var vendorLandedCost = parseFloat(vendorSearchresults[0].getValue('custentity_asg_landed_cost','vendor'));
vendorLandedCost = parseFloat(vendorLandedCost);
The last toFixed() call converts the result of the first parseFloat into a string.
Looks like you need to round the number to two decimal places, which is why you're using the parseFloat call. You can do something like this instead:
vendor_cost = Math.round(parseFloat(vendorSearchresults[0].getValue('vendorcost')) * 100) / 100
Well, Number.toFixed returns string because it is a data presentation function.
See the docs.
How can I compare two math expressions in JavaScript?
For example "1 + x" and "x + 1".
There is a JavaScript Math parser but I couldn't find the logic to compare the parsed expressions:
https://github.com/silentmatt/js-expression-eval/blob/master/test.js
Are there other options?
Thanks
You will need a computer algebra system (CAS) to be able to truly test whether two expressions are equal. The only one I know of in JavaScript is javascript-cas, I'm not sure if this does what you need though. Other libraries like math.js and the mentioned js-expression-eval can parse expressions but miss to logic to understand whether two expressions are equal.
A pragmatic approach may be to compare the outcome of both expressions for a number of different values for x, and see if they have the same result. This will not always work though, for example 0/x+1 and x-x give the same result for every x but they aren't really equal.
If you need to know just the equality then you can do approximate comparison:
extract variable names from expressions
you have x+1 and 1+x so there is single variable x
genere pseudo random or grid set of values for them
use some spec point from -inf to +inf like x={ -1e-10, 1e-9, .... 1e+9,1e+10 }
add few randoms
parse each expression with current variable set
so take first x value
compute expressions value for it
if it is not the same expresions are not the same
try all cases from genered data set
if all pass then expressions are likely the same
if not (even if just in one pass) then expressions are not the same
alert('g' - 'a') is returning Not a Number. ('NAN').
But I expect, to get the difference between ascii as alert(103-97) => alert(6). Hence 6 to be output.
In C, int i = 'g' - 'a', will give i = 6.
How to achieve this subtraction of 2 characters in javascript? (easily without much effort as below)
alert("g".charCodeAt(0) - "a".charCodeAt(0)) is giving 6.
Application : I am using this in chess program.
The only practicable way to do as you want is the way you've already suggested:
alert('g'.charCodeAt(0) - 'a'.charCodeAt(0));
As you know, this will retrieve the ASCII character code from 0th element of the string in each case, and subtract the second from the first.
Unfortunately this is the only way to retrieve the ASCII code of a given character, though using a function would be somewhat simpler, though given the brevity/simplicity of the charCodeAt() solution not all that much so.
References:
String.charCodeAt().
JavaScript doesn't treat characters as numbers; they are single-character strings instead. So the subtract operator will be calculating Number('g') - Number('a').
You should do 'g'.charCodeAt(0) - 'a'.charCodeAt(0) (there is no better way, but you can wrap it in a function)
You can write yourself a custom function. Something like this:
function asciiDif(a,b) {
return a.charCodeAt(0) - b.charCodeAt(0);
}
And then:
alert(asciiDif('g','a'));