How can I convert a string into a math operator in javascript [duplicate]

Asked
Active3 hr before
Viewed126 times

9 Answers

convertstring
90%

Without converting any of the strings in your array into integers or operators you could write,The eval function takes a string and then returns the value of that string considered as a math operation. For example, , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,This helps in your case because you have an array of strings that you want you want to treat as the mathematical operators and operands that they represent.

var math_it_up = {
   '+': function(x, y) {
      return x + y
   },
   '-': function(x, y) {
      return x - y
   }
}​​​​​​​;

math_it_up['+'](1, 2) == 3;
load more v
88%

This would turn myString into an array: ,Without converting any of the strings in your array into integers or operators you could write,which will return 240, the value of adding 225 to 15. ,This helps in your case because you have an array of strings that you want you want to treat as the mathematical operators and operands that they represent.

If I've got a string that is a mathematic equation and I want to split it and then calculate it. I know I can use the eval() function to do this, but I'm interested if there's an alternative way to do this - specifically by splitting the strings first. So I've got something like

var myString = "225 + 15 - 10"
var newString = myString.split(" ");

This would turn myString into an array:

["225", "+", "15", "-", "10"];
load more v
72%

eval(string)
65%

Introducing JavaScript objects, The eval() function evaluates JavaScript code represented as a string. ,eval() returns the value of the last expression evaluated.,A re-introduction to JavaScript

eval(string)
load more v
75%

Here are 6 ways to use the Spread operator with Array in JavaScript. You can use it to merge or clone an array. Or use it to convert iterables to an array.,Now we can use Spread to convert our node list into an array:,Using Spread, we can convert the set into an array:,The best thing about the Spread syntax is to use it for array manipulation 👏

// Merge Array
[...array1, ...array2]

// Clone Array
[...array]

// String → Array
[...'string']

// Set  → Array
[...new Set([1, 2, 3])]

// Node List → Array
[...nodeList]

// Arguments → Array
[...arguments]
load more v
40%

In the above syntax, … is spread operator which will target all values in particular variable. When … occurs in function call or alike,its called a spread operator. Spread operator can be used in many cases,like when we want to expand,copy,concat,with math object. Let’s look at each of them one by one:,Output:When …arr is used in the function call, it “expands” an iterable object arr into the list of argumentsIn order to avoid this NaN output, we make use of spread operator, like:,Whenever we want to expand an array into another we do something like this:,In the above code we can clearly see that when we tried to insert an element inside the array, the original array is also altered which we didn’t intended and is not recommended. We can make use of the spread operator in this case, like this:

Syntax:

var variablename1 = [...value];
load more v
22%

The JavaScript method toString() converts an array to a string of (comma separated) array values.,JavaScript automatically converts an array to a comma separated string when a primitive value is expected.,The concat() method creates a new array by merging (concatenating) existing arrays:,All JavaScript objects have a toString() method.

const fruits = ["Banana", "Orange", "Apple"];;
60%

Arithmetic expressions perform mathematic operations on numbers. Some arithmetic expressions can also support date arithmetic.,Returns the population covariance of two numeric expressions.,Returns the exponential moving average for the numeric expression.,Applies a regular expression (regex) to a string and returns a boolean that indicates if a match is found or not.

.leafygreen-ui-iokrs{color:inherit;font-size:13px;font-family:'Source Code Pro',Menlo,monospace;line-height:24px;}.leafygreen-ui-1v41da1{border-spacing:0;width:100%;}.leafygreen-ui-7razhx{border-spacing:0;vertical-align:top;padding:0 16px;}{ <operator>: [ <argument1>, <argument2> ... ] }
load more v
48%

work Software Engineer ,We can use the double tilde operator to convert the string to number. ,The unary plus operator (+) precedes its operand and evaluates to its operand but attempts to convert it into a number, if it isn't already.

myString = '129'
console.log(parseInt(myString)) // expected result: 129

a = 12.22
console.log(parseInt(a)) // expected result: 12
load more v

Other "convert-string" queries related to "How can I convert a string into a math operator in javascript [duplicate]"