# Evaluating Reverse Polish Notation (RPN) Expressions using Stack

[leetcode](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/)

**Problem**:

You are given an array of strings `tokens` that represents an arithmetic expression in a [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).

Evaluate the expression. Return *an integer that represents the value of the expression*.

**Note** that:

* The valid operators are `'+'`, `'-'`, `'*'`, and `'/'`.
    
* Each operand may be an integer or another expression.
    
* The division between two integers always **truncates toward zero**.
    
* There will not be any division by zero.
    
* The input represents a valid arithmetic expression in a reverse polish notation.
    
* The answer and all the intermediate calculations can be represented in a **32-bit** integer.
    

**Example 1:**

```plaintext
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
```

**Example 2:**

```plaintext
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
```

**Introduction**: Reverse Polish Notation (RPN) is a mathematical notation that places operators after their operands. In this blog post, we will explore how to evaluate RPN expressions using a stack-based approach. We will walk through the provided JavaScript code and explain the process step by step. So let's get started!

```plaintext
/**
 * @param {string[]} tokens
 * @return {number}
 */
var evalRPN = function(tokens) {
  let stack = [];
  let operators = ["+", "-", "*", "/"];
  for (let token of tokens) {
    if (operators.includes(token)) {
      if (token === "+") {
        let a = Number(stack.pop());
        let b = Number(stack.pop());
        stack.push(b + a);
      }
      if (token === "-") {
        let a = Number(stack.pop());
        let b = Number(stack.pop());
        stack.push(b - a);
      }
      if (token === "*") {
        let a = Number(stack.pop());
        let b = Number(stack.pop());
        stack.push(b * a);
      }
      if (token === "/") {
        let a = Number(stack.pop());
        let b = Number(stack.pop());
        stack.push(Math.trunc(b / a));
      }
    } else {
      stack.push(token);
    }
  }
  return Number(stack.pop());
};
```

**Explanation**: The given code provides an implementation to evaluate Reverse Polish Notation (RPN) expressions using a stack-based approach. Let's break it down step by step.

1. Initialize an empty `stack` array to store the intermediate results.
    
2. Define an `operators` array that contains the valid arithmetic operators: "+", "-", "\*", and "/".
    
3. Iterate over each `token` in the `tokens` array.
    
4. Check if the `token` is one of the operators using the `includes` method.
    
5. If the `token` is an operator:
    
    * Perform the corresponding arithmetic operation based on the operator.
        
    * Pop the last two operands from the `stack`, convert them to numbers using the `Number` function, and perform the operation.
        
    * Push the result back to the `stack`.
        
6. If the `token` is not an operator, assume it is an operand, and push it to the `stack` without any modification.
    
7. After processing all the tokens, the final result will be the only element remaining in the `stack`. Convert it to a number using the `Number` function and return it.
    

**Conclusion**: In this blog post, we explored how to evaluate Reverse Polish Notation (RPN) expressions using a stack-based approach. The Above JavaScript code effectively processes the tokens, performs the arithmetic operations, and returns the result. Reverse Polish Notation offers a concise and unambiguous way of representing mathematical expressions, and this implementation allows for efficient evaluation. Feel free to use this code as a reliable solution when working with RPN expressions. Happy coding!

Note: Feel free to modify the code as needed and provide appropriate context and examples to make the blog more comprehensive and informative.
