2

not really sure this is possible, cant seem to find any answers that cover it.

Ive found questions on running a math expression that's given as a string and they do the opposite of what im looking for.

basically im debugging my code and want to print out what the expression is to the console. There are currently 30 calculations in various expressions that run each time the code executes and the number is growing.

EG:

  var equation = (1 + 5) * (21 x 7);

Ive simplified it as there are variables that are actually parsed for the output.

I want to out put the expression as a string running the calculation but also keep the calculation running for the application.

I am currently concatenating strings and its tedious work so I hoped there might be a better solution than this: as you should see, i do need the variables to work as expected but not the whole expression, thus giving the simplified more readable result above.

This works with varuns answer but not in my case see update below:

var printout = '(' +var1+ ' + ' +var2+ ') * (' +var3+ ' * ' +var4+ ')';

Update based upon Varun's Answer & Comments below it:

var array = [1, 5, 21, 7];

var printout = '(' +array[0]+ ' + ' +array[1]+ ') * (' +array[2]+ ' * ' +array[0]+ ')';
CodingInTheUK
  • 930
  • 7
  • 16
  • do you want to evaluate this expression '(' +var1+ ' + ' +var2+ ') * (' +var3+ ' * ' +var4+ ')' as number – Ahmed Yousif Feb 26 '19 at 11:00
  • 1
    I guess he aims for the opposite. If the code is like `var var1 = 1; var var2 = 2; var equation = var1 + var2;`, he wants to print `equation` as `1 + 2`. In summary, the equation should be printed instead of the result. – Aycan Yaşıt Feb 26 '19 at 11:03
  • No ahmed, that is already occuring, I simply want to output a readable "calculation string" so when i look at the result I know exactly how the result was calculated. Its simply to speed up the debugging. – CodingInTheUK Feb 26 '19 at 11:04
  • Spot on Aycan, I have it doing just that with the concat but as I said, it is tedious and I know there are going to be many more expressions to do down the road. – CodingInTheUK Feb 26 '19 at 11:06
  • 1
    use string literals less tedious but still are var printout = \`(${var1}+${var2})*(${var3}*${var4})` – AZ_ Feb 26 '19 at 11:07
  • I tried that, it just prints out array[0], array[1] etc etc I need the variables (including array properties to function correctly. Basically I want to see the equation in the console looking like my first code example so i can see the numbers involved, I hope that makes it clearer. – CodingInTheUK Feb 26 '19 at 11:13
  • BACKTICKS! Sorry your commented answer works with array properties too. Just my attention to detail that failed. Both are very good answers and actually yours is the simpler of the two. – CodingInTheUK Feb 27 '19 at 01:10

2 Answers2

2

I guess below is the code you are looking for

 var eq = `(1 + 5) * (21 * 7)`;

console.log( eq + ' = ' + eval(eq) )

You can also enhance it like this for more dynamic content

var array = [1, 5, 21, 7];
var inp1 = array[0]
var inp2 = array[1]
var inp3 = array[2]
var inp4 = array[3]
var addOperand = "+"
var multiplyOperand = "*"

var eq = `(${inp1} ${addOperand} ${inp2}) ${multiplyOperand} (${inp3} ${multiplyOperand} ${inp4})`;

console.log( eq + ' = ' + eval(eq) )

Since you are using arrays, this is seems scalable and efficient

var array = ['(',1, '+', 5,')', '*','(', 21,'*', 7,')'];

eq = array.join(' ')

console.log( eq + ' = ' + eval(eq) )
1

Could be more generic but not sure if you need that much only for testing, here your equation can be any string.

lineNo is the lineNo of your equation inside the function

function calculate(x, y) {
 var equation = (x + 5) * (y * 7);
 let lineNo = 1
 console.log(arguments.callee.toString().split('\n\t')[lineNo].replace(/x/g, x).replace(/y/g, y))
}

calculate(3, 4)
AZ_
  • 3,094
  • 1
  • 9
  • 19
  • For my purposes you have hit the mark, as its already a function im working in I didnt do this entirely but used the let and console lines, I just have to count how many lines and adjust the replaces for the query but this is very workable, I am going to switch my accepted answer as this is working better for me. – CodingInTheUK Feb 26 '19 at 12:21
  • 1
    glad it helped, you don't need let lineNo just directly use the value in console expression. I added only for the explanation. – AZ_ Feb 26 '19 at 12:23