Javascript Algorithm String Validate Bracket Expression

Description

Javascript Algorithm String Validate Bracket Expression


function validateBraketExpression(exp){
    var len = exp.length;
    var arr = new Array();
    for (var index = 0; index<len; index++){
        var ch = exp.charAt(index);
        if(ch=='{'||ch=='['||ch=='('){
            arr.push(ch);/*from w  w  w  . jav a  2 s .c  o  m*/
        }else if(ch=='}'||ch==']'||ch==')'){

            var lastBraket = arr.pop();

            if(!((lastBraket == '{' && ch =='}') ||
                (lastBraket == '[' && ch ==']') ||
                (lastBraket == '(' && ch ==')'))){
                return "Invalid Expression";
            }
        }
    }

    if(arr.length == 0){
        return "Valid Expression";
    }
    else{
        return "Invalid Expression";
    }
}

var expression = "5*{(2+3)+[(3+5)/5}]";
var result = validateBraketExpression(expression);
console.log(result);



PreviousNext

Related