Infix to Postfix Converter using JavaScript

Postfix expressions are easier for a compiler to understand and evaluate. So this is a converter that converts infix expression to postfix expression using JavaScript.
Pre-requisites:
- Stack operation
- Infix to Postfix conversion
- Basic JavaScript
Approach:
- Button Convert call function InfixtoPostfix() and this function converts the given infix expression using JavaScript.
- Display the Postfix expression.
HTML code:
HTML
| <!DOCTYPE html><htmllang="en"><head>    <!-- link script.js file -->    <scripttype="text/javascript"src="script.js"></script>    <metacharset="UTF-8">    <metahttp-equiv="X-UA-Compatible"content="IE=edge">    <metaname="viewport"content=        "width=device-width, initial-scale=1.0">    <linkhref=        rel="stylesheet"        integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"        crossorigin="anonymous">    <!-- Infix to Postfix & Prefix Converter title added  -->    <title>Infix to Postfix & Prefix Converter</title>    <!-- link style.css file -->    <linkhref="infix.css"rel="stylesheet"></head><body>    <navclass="navbar navbar-light bg-light">        <divclass="container-fluid"            style="justify-content: center;">            <spanclass="navbar-brand mb-0 h1">                Infix to Postfix Converter in JavaScript            </span>        </div>    </nav>    <divclass="inputdata">        <!-- Taking infix expression as input -->        <spanclass="span1">            Enter Valid Infix Expression        </span>        <br>        <inputtype="text"placeholder="Infix Expression"            id="infixvalue"class="input1">        <br>        <!-- InfixtoPostfix() function call-->        <buttononclick="InfixtoPostfix()"class="btn1">            Convert</button>    </div>    <br>    <divclass="output">        <!-- Postfix expression printed -->        <spanstyle="font-size:1.4vw;  font-size: 2.2vw;">            Postfix Expression:-            <spanid="text"                style="color: black; font-weight: 600;">            </span>        </span>    </div></body></html> | 
CSS code: The following code is the content for “infix.css” file used in the above HTML code.
CSS
| body {    background-color: #fff0c3;}.container-fluid {    background-color: #6f459e;    justify-content: center;}.navbar-brand mb-0h1{    color: black;}.navbar-light .navbar-brand {    color: white;}.navbar navbar-light bg-light {    width: 100%;}.navbar {    padding: 0%;}.navbar-brand {    font-size: 1.8rem;}.navbar-light .navbar-brand:hover {    color: white;}.inputdata {    text-align: center;    margin-top: 21vh;}.span1{    font-size: 2vw;    font-weight: 500;    color: black;}.input1{    width: 58vw;    text-align: center;    height: 3vw;    place-items: center;    font-size: 20px;    border: 2pxsolidwhite;    border-radius: 18px;    margin-top: 2vw;}.btn1{    border: wheat;    background-color: #6f459e;    text-align: center;    color: white;    font-weight: 500;    border-radius: 8px;    margin-top: 1.5vw;    width: 7vw;    height: 6vh;}button:hover {    background-color: #c694ff;}input:focus {    outline: none;}.output {    text-align: center;} | 
JavaScript code: The following code is the content for the file “script.js” used in the above HTML code.
Javascript
| // Created an empty arrayvarstackarr = [];// Variable topp initialized with -1vartopp = -1;// Push function for pushing // elements inside stackfunctionpush(e) {    topp++;    stackarr[topp] = e;}// Pop function for returning top elementfunctionpop() {    if(topp == -1)        return0;    else{        varpopped_ele = stackarr[topp];        topp--;        returnpopped_ele;    }}// Function to check whether the passed// character is operator or notfunctionoperator(op) {    if(op == '+'|| op == '-'||         op == '^'|| op == '*'||         op == '/'|| op == '('||         op == ')') {        returntrue;    }    else        returnfalse;}// Function to return the precedency of operatorfunctionprecedency(pre) {    if(pre == '@'|| pre == '('|| pre == ')') {        return1;    }    elseif(pre == '+'|| pre == '-') {        return2;    }    elseif(pre == '/'|| pre == '*') {        return3;    }    elseif(pre == '^') {        return4;    }    else        return0;}// Function to convert Infix to PostfixfunctionInfixtoPostfix() {    // Postfix array created    varpostfix = [];    vartemp = 0;    push('@');    infixval = document.getElementById("infixvalue").value;    // Iterate on infix string    for(vari = 0; i < infixval.length; i++) {        varel = infixval[i];        // Checking whether operator or not        if(operator(el)) {            if(el == ')') {                while(stackarr[topp] != "(") {                    postfix[temp++] = pop();                }                pop();            }            // Checking whether el is (  or not            elseif(el == '(') {                push(el);            }            // Comparing precedency of el and            // stackarr[topp]            elseif(precedency(el) > precedency(stackarr[topp])) {                push(el);            }            else{                while(precedency(el) <=                     precedency(stackarr[topp]) && topp > -1) {                    postfix[temp++] = pop();                }                push(el);            }        }        else{            postfix[temp++] = el;        }    }    // Adding character until stackarr[topp] is @    while(stackarr[topp] != '@') {        postfix[temp++] = pop();    }    // String to store postfix expression    varst = "";    for(vari = 0; i < postfix.length; i++)        st += postfix[i];    // To print postfix expression in HTML    document.getElementById("text").innerHTML = st;} | 
Output:
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
 
				 
					



