How to resolve the algorithm Arithmetic evaluation step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic evaluation step by step in the JavaScript programming language

Table of Contents

Problem Statement

For those who don't remember, mathematical precedence is as follows:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Arithmetic evaluation step by step in the JavaScript programming language

The provided JavaScript code defines a function named evalArithmeticExp that evaluates arithmetic expressions given as a string. It handles mathematical operations such as addition, subtraction, multiplication, and division, and can evaluate expressions that include parentheses.

Here's a detailed breakdown of the code:

  • evalArithmeticExp(s) function:

    • This is the main function that takes an arithmetic expression as a string (s) and evaluates it.
    • It first removes any white spaces and any leading "+" sign from the input string.
    • It sets up a regular expression rePara to match any parentheses in the string.
    • It enters a loop that continues as long as there are parentheses in the string.
    • Inside the loop, it replaces any parentheses in the string with the result of evaluating the expression within the parentheses using the evalExp function.
  • evalExp(s) function:

    • This function evaluates a single arithmetic expression without parentheses.
    • It removes any parentheses from the input string.
    • It sets up regular expressions to match different types of operations:
      • reMD: Matches multiplication or division operations.
      • reM: Matches multiplication operations.
      • reAS: Matches addition or subtraction operations.
      • reA: Matches addition operations.
    • It enters a loop that continues as long as there are matches for multiplication or division operations.
    • Inside the loop, it replaces the matched expression with the result of performing the multiplication or division operation using the multiply or divide function.
    • After that, it enters another loop that continues as long as there are matches for addition or subtraction operations.
    • Inside the loop, it replaces the matched expression with the result of performing the addition or subtraction operation using the add or subtract function.
  • Helper functions:

    • The multiply, divide, add, and subtract functions are helper functions that perform the actual arithmetic operations. They take the input string and split it into operands based on the operation type.
  • Usage:

    • To use this code, you can call the evalArithmeticExp function and provide an arithmetic expression as a string. It will return the evaluated result of the expression.

Overall, this code provides a way to evaluate arithmetic expressions in JavaScript, including expressions with parentheses and various arithmetic operations.

Source code in the javascript programming language

function evalArithmeticExp(s) {
  s = s.replace(/\s/g,'').replace(/^\+/,'');
  var rePara = /\([^\(\)]*\)/;
  var exp = s.match(rePara);

  while (exp = s.match(rePara)) {
    s = s.replace(exp[0], evalExp(exp[0]));
  }
  return evalExp(s);
  
  function evalExp(s) {
    s = s.replace(/[\(\)]/g,'');
    var reMD = /\d+\.?\d*\s*[\*\/]\s*[+-]?\d+\.?\d*/;
    var reM = /\*/;
    var reAS = /-?\d+\.?\d*\s*[\+-]\s*[+-]?\d+\.?\d*/;
    var reA  = /\d\+/;
    var exp;

    while (exp = s.match(reMD)) {
      s = exp[0].match(reM)? s.replace(exp[0], multiply(exp[0])) : s.replace(exp[0], divide(exp[0]));
    }
    
    while (exp = s.match(reAS)) {
      s = exp[0].match(reA)? s.replace(exp[0], add(exp[0])) : s.replace(exp[0], subtract(exp[0]));
    }
    
    return '' + s;

    function multiply(s, b) {
      b = s.split('*');
      return b[0] * b[1];
    }
    
    function divide(s, b) {
      b = s.split('/');
      return b[0] / b[1];
    }
    
    function add(s, b) {
      s = s.replace(/^\+/,'').replace(/\++/,'+');
      b = s.split('+');
      return Number(b[0]) + Number(b[1]);
    }
    
    function subtract(s, b) {
      s = s.replace(/\+-|-\+/g,'-');

      if (s.match(/--/)) {
        return add(s.replace(/--/,'+'));
      }
      b = s.split('-');
      return b.length == 3? -1 * b[1] - b[2] : b[0] - b[1];
    }
  }
}


  

You may also check:How to resolve the algorithm Bulls and cows/Player step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the 11l programming language
You may also check:How to resolve the algorithm FTP step by step in the Nim programming language
You may also check:How to resolve the algorithm Add a variable to a class instance at runtime step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Red programming language