Skip to main content

Evaluating Prefix expressions in JavaScript

 

Question: Prefix expression evaluation


A string containing the prefix expression is given to you. Evaluate it and print the single integer giving the answer.

Input Description:
You are given a string ‘s’.

Output Description:
Print the evaluated answer of that string.

Sample Input:
+23
Sample Output:
5

Answer: 


Steps to evaluate a prefix expression:

  1. Split the prefix expression based on space (" ") or blank string ("") as per the given input.
  2. Store the split characters in an array and reverse the array.
  3. Create an evaluation array with no elements (blank array).
  4. Iterate through the elements of the characters array.
  5. If the character is a number, simply push the character into the evaluation array.
  6. if the character is an operator (+, -, *, /, %), then pop 2 elements from the evaluation array. Store the first popped number as the left Operand and the second as the right Operand. 
  7. Calculate the result using the operands and current operator. [left operand (operator) right operand]. You may use switch-case or if-else statements to recognize the operator.
  8. After calculating the result push the result into the evaluation array.
  9. After iterating through all the elements of the characters array, the evaluation Array will have only one number, resulting from the evaluation of the prefix expression.
  10. Pop the element and return it as the output.




Comments

Popular posts from this blog

Understanding the Difference Between Document and Window Objects

JavaScript Object Introduction: When it comes to building websites, knowing the difference between Document and Window objects in JavaScript is important. These objects work together to make web pages interactive, but they have distinct roles. In this blog, we'll explore what Document and Window objects do and how they differ from each other, and we'll provide code snippets to illustrate their functionalities. Document Object: The Document object represents the web page itself. It lets you access and change the content and structure of the page. When you want to interact with the elements on a page using JavaScript, you'll be working with the Document object. Key features of the Document object include: ...

Mastering Git: The Ultimate Guide to Version Control with VSCode

Introduction: In the world of software development, efficient collaboration, and seamless version control are essential for successful project management. Git, a distributed version control system, has revolutionized the way developers work by providing a robust and flexible solution. In this blog post, we will explore the fundamentals of Git, its significance in modern software development, and how to leverage Git commands using the popular IDE, Visual Studio Code (VSCode). What is Git and Why is it Used? Git is a free and open-source distributed version control system designed to handle everything from small to large projects with speed and efficiency. It allows multiple developers to work on the same project simultaneously, providing seamless integration and a structured approach to managing changes. Git offers several key benefits, including: Distributed Version Control: Each developer has a complete copy of the project's history, enabling them to work offline and independentl...

HTTP 1.1 vs HTTP 2

HTTP 1.1 vs HTTP 2 Introduction: HTTP (Hypertext Transfer Protocol) is the underlying protocol that powers the World Wide Web. It is a request-response protocol that enables communication between clients and servers. Over the years, HTTP has evolved and seen several versions. The two most widely used versions of HTTP are HTTP1.1 and HTTP2. In this blog, we will explore the differences between HTTP1.1 and HTTP2. Multiplexing: One of the significant differences between HTTP1.1 and HTTP2 is that HTTP2 supports multiplexing, while HTTP1.1 doesn't. Multiplexing allows multiple requests and responses to be sent and received concur...