0

Unexpected token error in java script and want to see is it Working perfectly or not

I am getting error in my script and I am newbie so I am not able to recognize what it is and is this script is working fine or nor so

And the script is for Calculator so Check is it proper or not.

JAva SCript

var decimalPoint = enter = entered = operatorSign = rootNpower_Sign = flo = math = M = firstI = first = second=second1 = answer = theanswer = result = peSign ="";
 var opsCheck = dotCounter = 0;
      var removeFirstZero="";
function dot(b){
    if(decimalPoint ==""){
        enter = entered=b;
        first+=enter;
        entered+=enter;
        decimalPoint = ".";
        dotCounter = 0;
        return first;
    }else{
        return first;
        
    }
}

//Control what happened when Pi and euler is click
function PE(b){
    decimalPoint=".";
    dotCounter =15;
    var cons =b;
    
    if (peSign == ""){
        if(operatorSign !="" && first=="" + operatorSign){
            first =(cons=""PI)? Math.PI:Math.E;
        }
        else if(operatorSign != "" && first >0 || first <0){
            first =(cons == "PI")?first + "*" +Math.E;
        }
            else if(first!="" && operatorSign!=""){
            first+=(cons = "PI")? Math.PI:Math.E;
        }
        else if(operatorSign =="" && first !=""){
            first=(cons=="PI")? first+"*" +Math.PI:first+"*"+Math.E;
            
        }
        else{
            first=(cons=="PI")?Math.PI:Math.E;
        }
        return first;
    }
    function mp(){
        M =first;
        first=M;
        return first;
    }
    function mr(){
        first = M;
        return first;
    }
    
    function c(c){
        document.getElementById("display1").innerHTML = decimalPoint=operatorSign=rootNpower_Sign=entered=math first=firstI=second=second1=answer=theanswer=flo=M="";
        return "";
    }
    function mathematics()
    {
        if(math=="sqr"){
            result=firstI*firstI;
        }else if(math=="cube"){
            result=firstI*firstI*firstI;
            
        }
        else if(math=="sqrt"){
            result=Math.sqrt(firstI);
        }
        else if(math=="cubert"){
            result=Math.cubert(firstI);
        }
        else if(math =="negpos"){
            result=firstI*-1;
        }
        else if(math=="sine"){
            result=Math.sin(firstI);
        }
        else if(math=="cosine"){
            result=Math.cos(firstI);
        }
        else if(math=="tangent")
            {
                result=Math.tan(firstI);
            }
        else if(math =="ln"){
            result=Math.log(firstI);
        }
        else if(math=="logTen"){
            result=Math.log10(firstI);
        }
        else if(math=="rand"){
                result=Math.round(firstI);
                }
        else if(math=="res")
{
    result=1/firstI;
                
                }
        else if(math=="fact"){
            n=firstI;
            firstI=1;
            while(n>1){
                firstI*=n;
                n-+1;
            }
            result=firstI;
        }decimalPoint=(Math.round(result)==result)? "": ".";
    }
    function prep(){
        second=eval(second);
        first=eval(first);
    }
    function maths(a){
        math=a;
        try{
            if(operatorSign=="+"){
                prep();
                firstI=first-second;
                mathematics();
                first=second+"+"+result;
            }
            else if(operatorSign=="-"){
                prep();
                firstI=second-first;
                mathematics();
                first=second+"-"+"("+result")";
            }
            else if(operatorSign=="*"){
                prep();
                firstI=second/first;
                mathematics();
                first=second+"/"+result;
            }
            else{
                firstI=first;
                mathematics();
                first=result;
            }
            return first;
        }
    }
    function digit(b){
        opsCheck=0;
        dotCounter++;
        if(first==Infinity || first ==NaN){
            first=0;
        }
        peSign="pes";
        entered=b;
        if(rootNpower_Sign !=""){
            first=(first==="0" && entered:first+entered);
            return second1 +firstI*rootNpower_Sign*first;
        }
        else{
            first=(first==="0" && entered:first+entered);
            theanswer=eval(first)+"";
            if(theanswer.length>14){
                theanswer=Math.abs((theanswer*1).toPrecision(14));
            }
            document.getElementById("display1").innerHTML=first;
            return theanswer;
        }
    }
    var thebase ="";
    function base(a){
        thebase=a;
        first*=1;
        if(thebase =="bin"){
            firstII=first.topString(2);
        }else if(thebase=="oct"){
            firstII=first.topString(8);
        }
        else if(thebase=="hex"){
            firstII=first.topString(16);
        }
        return firstII;
    }
    function themes(thm){
        theme=thm;
        el=document.getElementsByClass("div");
        if(theme==1){
            el[0].id ="theme1";
            }
        else if(theme==2){
            el[0].id="theme2";
        }
        else if(theme==3){
            el[0].id="theme3";
        }
        else {
            el[0].id="theme4";
        }
    }
    //backspace
    function bsp()
    {
        first +="";
        dotCounter--;
        decimalPoint=dotCounter>=0 ? "." :"";
        first=first.substr(0,first.length-1);
        document.getElementById("display1").innerHTML =-first;
        try{
            eval(first);
            return first;
        }
        catch (first){
            eval(first);
            return "";
        }
    }
    funvction pow(){
        first*=1;
        result == Math.pow(firstI,first);
    }
    function roots(){
        first *=1;
        result=Math.pow(first,1/firstI).toPrecision(12);
        result=Math.abs(result);
    }
    function operator(b){
        peSign ="";
        if (opsCheck==0){
            opsCheck =1;
            document.getElementById("display1").innerHTML = first;
            try{
                if(rootNpower_Sign =="^"){
                    if(operatorSign=="+"){
                        pow();
                        answer=result+second;
                    }
                    else if(operatorSign=="-"){
                        pow();
                        answer=second-result;
                    }
                    else if(operatorSign=="*"){
                        pow();
                        answer=result*second;
                    }
                    else if(operatorSign=="/")
                          {
                              pow();
                              answer=second/result;
                          }
                    else{
                        pow();
                        answer=result;
                    }
                } if(rootNpower_Sign=="√"){
                    if(operatorSign=="+"){
                        roots();
                        answer=second+result;
                    }else if(operatorSign=="-"){
                        roots();
                        answer=second-first;
                    }else if(operatorSign=="*"){
                        roots();
                        answer=result*second;
                    }else if(operatorSign=="/"){
                        roots();
                        answer=second/result;
                    }else{
                        roots();
                        answer=result;
                    }
                }else if(a=="%"){
                    answer=second%first;
                }else{
                    operatorSign=b;
                    first+=operatorSign;
                    decimalPoint="";
                }
                rootNpower_Sign="";
                operatorSign=b;
                firstI="";
                second=answer;
                first=answer+operatorSign;
                decimalPoint="";
                document.getElementById("display1").innerHTML =first;
                return ecval(second);
            }catch(x){
                if(first !="<span class='red'>Press ON to start</span>"){
                    operatorSign=b;
                    second=eval(first);
                    first+=operatorSign;
                    decimalPoint="";
                }else{
                    first="<span class='red' Press ON to Start</span>";
                }
                document.getElementById("display1").innerHTML=first;
                return(second==undefined)? 0:eval(second);
            }
            else{
                operatorSign=b;
                first+="";
                first=first.substr(,first.length-);
                first=first+operatorSign;
                document.getElementById("display1").innerHTML=first;
                return(second==undefined)? 0:eval(second);
                }
        }
        function percent(){
            first=eval(first)*100;
            return first *"%";
        }
        function negpos(){
            first=(operatorSign=="")?first*=-1:first;
            return first;
        }
        function power(b){
            rootNpower_Sign=b;
            if(operatorSign=="+" && second != ""){
                prep();
                first=first-second;
                first="";
                second1=second+ "+";
                return second + "+" firstI+rootNpower_Sign;
            }
            else if(operatorSign=="-" && second !="")
                {
                    prep();
                    firstI=first/second;
                    first="";
                    second1=second+ "*";
                    return second + "-" firstI+rootNpower_Sign;
                }
            else if(operatorSign =="*" && second !=""){
                prep();
                firstI=second-first;
                first="";
                second1=second+"-";
                return second * "*" +firstI+rootNpower_Sign;
            }
           else if(operatorSign =="/" && second !=""){
                prep();
                firstI=second/first;
                first="";
                second1=second+"/";
                return second * "/" +firstI+rootNpower_Sign;
            }
            else
                {
                    rootNpower_Sign=b;
                    firstI=first;
                    first="";
                    return= firstI + rootNpower_Sign;
                }
        }
        function equal(){
            document.getElementById("display1").innerHTML=first;
            try{
                if(rootNpower_Sign =="^"){
                    if(operatorSign=="+"){
                        pow();
                        first=result+second;
                    }
                    else if(operatorSign=="-"){
                        pow();
                        first=second-result;
                    }
                    else if(operatorSign=="*"){
                        pow();
                        answer=result*second;
                    }
                    else if(operatorSign=="/"){
                        pow();
                        first=second / result;
                    }
                    else{
                        first=result;
                    }
                }else if(rootNpower_Sign=="√"){
                    if(operatorSign=="+"){
                        roots();
                        first=result+second;
                    }else if(operatorSign=="-"){
                        roots();
                        first=second - result;
                    }
                    else if(operatorSign=="*"){
                        roots();
                        first=result*second;
                    }
                    else if(operatorSign=="/"){
                        roots();
                        first=second / result;
                    }
                    else{
                        roots();
                        first=result;
                    }
                }else if(operatorSign=="%"){
                    answer = second % first;
                }else{
                    if(first==""){
                        first=first;
                    }else{
                        try{
                            first=eval(first)+ "";
                            if(first.length >14){
first=Math.abs((first * 1).toPrecision(14));
                            }
                        }
                        catch(first){
                           first="<small><small> Incorrect Input.Click c to Clear </small></small>";
                            return first;
                            first "";
                        }
                    }
                }
                rootNpower_Sign=operatorSign=answer=firstI=second="";
                flo=first;
                flo=Math.floor(flo);
                decimalPoint=(flo==first) ? "" : ".";
                return first;
            }
            catch(operatorSign){
                operatorSign="";
                first=eval(first) + "";
                if(first.length >14){
                  first=Math.abs((first*1).toPrecision(14));
                }
                flo=first;
                flo.match.floor(flo);
                decimalPoint = (flo==first)? "" :".";
                return first;
            }
        }
    }
}

26 ERROR: Parsing error: Unexpected token PI first =(cons=""PI)? Math.PI:Math.E;

HTML CODE

<html>
    <head>
        <meta charset="utf-8">
        <title>Scientific calculator</title>
        <link rel="stylesheet" type="text/css" href="style2.css">
    </head>
    <body>
        <div class="container">
            <div class="div">
                <table>
                    <caption><pre></bsp></caption>
                <tr>
                    <td colspan="5">
                    <div id="display-container">
                        <div class="display1">
                            <small><small><span>
                                </span></small></small></div>
                        <div id="h1"><small><small><span></span></small></small>
                        </div>
                        </div>
                                
                        </td>
                    </tr>
                    
                    <tr>
<td><button class="top"onclick="getElementById("h1").innerHTML=bsp()">DEL</button></td>
<td><button class="top"onclick="getElementById("h1").innerHTML=mp()">M+</button></td>
<td><button class="top"onclick="getElementById("h1").innerHTML=mr()">MR</button></td> 
<td><button class="top"onclick="getElementById("h1").innerHTML=maths("rand")">Rnd</button></td>
                        <td rowspan="2"><button id="c" class="top" onclick="getElementById("h1").innerHTML=c("")"> C </button></td>                        
                                        
                    </tr>
                    <tr>
                        <td><button class="top"onclick="getElementById("h1").innerHTML=maths("res")"><sup>1</sup>/<sub>10</sub></button></td>
                        <td><button class="top"onclick="getElementById("h1").innerHTML=maths(logTen)"><small>log10</small></button></td>
<td><button class="top"onclick="getElementById("h1").innerHTML=maths(ln)">ln</button></td> <td><button class="top"onclick="getElementById("h1").innerHTML=PE("E")">e</button></td>                       
                    </tr>
    <tr>
        <td><button class="top"onclick="getElementById("h1").innerHTML=maths("sine")">sin</button></td>
<td><button class="top"onclick="getElementById("h1").innerHTML=maths("cosine")">cos</button></td>
<td><button class="top"onclick="getElementById("h1").innerHTML=maths("tangent")">tan</button></td>
<td><button class="top"onclick="getElementById("h1").innerHTML=PE("PI")">π</button></td>
<td><button class="top"onclick="getElementById("h1").innerHTML=percent()">%</button></td>
</tr>
                    <tr>
                        <td><button class="top"onclick="getElementById("h1").innerHTML=maths("fact")">n!</button></td>
                        <td><button class="top"onclick="getElementById("h1").innerHTML=digit("%")">Mod</button></td>
                        <td><button class="top"onclick="getElementById("h1").innerHTML=power("^")">x<sup>y</sup></button></td>
                        <td><button class="top"onclick="getElementById("h1").innerHTML=power("√")"><sup>y</sup>√x</button></td>
 
 <td><button class="top"onclick="getElementById("h1").innerHTML=maths("cubert")"><sup>3</sup>√</button></td>
                    </tr>
                    
<tr>
     <td><button class="top"onclick="getElementById("h1").innerHTML=digit("(")">(</button></td>
         <td><button class="top"onclick="getElementById("h1").innerHTML=digit(")")">)</button></td>
         <td><button class="top"onclick="getElementById("h1").innerHTML=base("bin")">bin</button></td>
    <td><button class="top"onclick="getElementById("h1").innerHTML=base("oct")">oct</button></td>
    <td><button class="top"onclick="getElementById("h1").innerHTML=base("hex")">hex</button></td>
                    </tr>
<tr>
<td><button class="top"onclick="getElementById("h1").innerHTML=digit("7")">7</button></td>
    <td><button class="top"onclick="getElementById("h1").innerHTML=digit("8")">8</button></td>
 <td><button class="top"onclick="getElementById("h1").innerHTML=digit("9")">9</button></td>                  
 <td><button class="top"onclick="getElementById("h1").innerHTML=operators("*")">x  </button></td>   
  <td><button class="top"onclick="getElementById("h1").innerHTML=maths("negpos")">-/+</button></td>   
    
                    </tr>    
<tr>
    <td><button class="top"onclick="getElementById("h1").innerHTML=digit("4")">4</button></td>
    <td><button class="top"onclick="getElementById("h1").innerHTML=digit("5")">5</button></td>
 <td><button class="top"onclick="getElementById("h1").innerHTML=digit("6")">6</button></td>                  
 <td><button class="top"onclick="getElementById("h1").innerHTML=operators("-")">-</button></td>   
  <td><button class="top"onclick="getElementById("h1").innerHTML=maths("sqrt")">√</button></td>   
                    
                    
                    </tr>
<tr>
    <td><button class="top"onclick="getElementById("h1").innerHTML=digit("1")">1</button></td>
    <td><button class="top"onclick="getElementById("h1").innerHTML=digit("2")">2</button></td>
 <td><button class="top"onclick="getElementById("h1").innerHTML=digit("3")">3</button></td>                  
 <td><button class="top"onclick="getElementById("h1").innerHTML=operators("/")">/</button></td>   
    <td><button class="top"onclick="getElementById("h1").innerHTML=maths("cube")">x<sup>3</sup></button></td>   
                    </tr>
                    <tr>
                        <td><button class="top"onclick="getElementById("h1").innerHTML=dot(".")">.</button></td>
    <td><button class="top"onclick="getElementById("h1").innerHTML=digit("0")">0</button></td>
 <td><button class="top"onclick="getElementById("h1").innerHTML=equal()">=</button></td>                  
 <td><button class="top"onclick="getElementById("h1").innerHTML=operators("+")">+</button></td>   
  <td><button class="top"onclick="getElementById("h1").innerHTML=maths("sur")">x<sup>2</sup></button></td>   
                    </tr>
                    
                </table>
            </div>
        </div>
        
    </body>
</html>
  • `first =(cons == "PI")?first + "*" +Math.E;` is your next syntax error. The conditional operator needs both a `?` and a `:`. `math first` is another error. This just doesn’t make sense. `Math.cubert` doesn’t exist. Did you mean `Math.cbrt`? Please revisit the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript) and the [JS guide](https://developer.mozilla.org/en-US/docs/Learn/JavaScript). – Sebastian Simon Nov 23 '20 at 08:50
  • Inline event handlers like `onclick` or `oninput` are [not recommended](https://stackoverflow.com/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](https://stackoverflow.com/a/43459991/4642212) way of registering events. Always [use `addEventListener`](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Inline_event_handlers_—_dont_use_these) instead. The quote nesting in `onclick="getElementById("h1").innerHTML=bsp()"` is invalid. – Sebastian Simon Nov 23 '20 at 08:51

1 Answers1

0

The line first+=(cons = "PI")? Math.PI:Math.E; should be first+=(cons == "PI")? Math.PI:Math.E;

Harshana
  • 5,151
  • 1
  • 17
  • 27