Help me javascript arith

The thing is

Var a = 7;    
Var b = '+' ;
Var c = 2;

Var f = a += b += c ;

Var f now gives ‘7+2’ as strings. Now i need to get 9 so i tried eval(parseInt(f)) i get 7 instead of 9. Any help with howi can get 9 ???

You don’t need to parse your string, eval will already evaluate the operation for you

Now the real question is… why?
Also var is spelled with the v in lowercase :wink:

Thanks for the reply… I didn’t copy and paste i typed this question on my mobile phone so pardon the upper case in the V. And that question is just a mere instance of what am trying to replicate. Am working on a calculator. So am using eval to evaluate long lines like 7*9/2+1 . Or do you know any other function that works like eval and its best for this ?

eval works fine, and is the only thing you can use where you put a string in and get it evaluate, but note that it won’t teach you much (in practise you are unlikely to ever use eval in a professional setting except for very specialised use cases).

Here is a very rough algorithm for one way of doing this (it is very rough and I haven’t thought about it much so don’t just take this a being a great way to do it):

  1. User presses number
  2. push that number to array
  3. User presses operator (eg ‘+’)
  4. unshift that to array
  5. User presses number
    6 push that number to array
  6. User presses ‘=’
  7. Array looks like [operator, number, number]
  8. Run a function that takes that array, checks what the operator is, and runs a relevant code block, for example:
    function calculate (input) {
      switch (input[0]) {
        case '+':
          return input[1] + input[2];
        case '-':
          /* etc... */
      }
    }
    

“polish notation calculator” and “shunting yard algorithm” are good things to Google here

1 Like

This is very thoughtful but the downside is that accessing array with [] is not flexible enough because input[0] might be anything lets say - or number i wont always know. Looping and switching should work tho but still in the end how do you suggest i evaluate line like this 2+8-1/9. NB: am replicating my phone calculator.

You should always know: you control how the input goes in, it isn’t arbitrary. If it’s an operator, then you check its an operator. If it’s an operator preceded by another operator, evaluate. If it’s a number, either push the number, or if it’s a number preceded by a number, you need to concatenate it to the previous number (eg 1 followed by 1 means you need to change the current number at the top of the stack from 1 to 11). You should always know, otherwise the calculator won’t work if you use a stack-based method or you eval a string, bad input is bad input regardless of what method is used, and you can control that input.

It’s maybe simpler if I separate the operators and the values into two arrays, and I’ll always push. Note this makes it flawed because it doesn’t take into account operator precedence, so a proper Polish notation calculator is better, but I’m doing this off the top of my head

operators = ['+', '-', '÷']
values = [2, 8, 1, 9]

Press equals to evaluate.

First operation is addition. Shift it off. Shift off the first two values, add them, unshift the result.
First operation is now minus. Shift it off. Shift off the first two values, subtract the second from the first, unshift the result.
First operation is now divide. Shift it off. Shift off the first two values, divide the first by the second, return the result.