简体   繁体   中英

My if statment and else if statment is not getting executed, but there is not error

So i'm writing a calculator and I'm struggling with the equal sign, I'm trying to do some simple bedmas (no brachets and no exponents), but my code keeps returning 0 as if the if statment ar egetting totally skipped

I have no idea what is causing this as I'm getting no errors

function equal(){
        let exp = document.form.textview.value;
        let expArray = exp.split(/\b/);
        console.log(expArray);   //<-if something doesn't work check this
        let total = 0;
        var lastOperator = "+";
        var lastDig = 0;
        for(let i = 0 ; i < expArray.length; i++){
          var dig = expArray[i].trim(); //value of "i", the item currently in the loop
          if(isNaN(dig) == true){
             console.log("string")
           }
          else if(isNaN(dig) == false){
            console.log('not string');
              if (dig == "/"){ //this whole section gets skipped :(
                var a = i + 1;
                var p = i - 1;
                var aDig = parseFloat(expArray[a].trim); //value after "i"
                var bDig = parseFloat(expArray[p].trim); // value before  "i"
                var sDig = aDig / bDig;
                total = expArray.splice(bDig, 3, sDig);
              }

              else if ( dig == "*"){
                var a = i + 1;
                var p = i - 1;
                var aDig = parseFloat(expArray[a].trim); //value after "i"
                var bDig = parseFloat(expArray[p].trim) // value before  "i"
                var sDig = aDig * bDig;
                total = expArray.splice(bDig, 3, sDig);
              }
              else if ( dig == "+"){
                var a = i + 1;
                var p = i - 1;
                var aDig = parseFloat(expArray[a].trim); //value after "i"
                var bDig = parseFloat(expArray[p].trim)// value before  "i"
                var sDig = aDig + bDig;
                total = expArray.splice(bDig, 3, sDig);
                console.log(expArray.splice(bDig, 3, sDig));
              }
              else if( dig == "-"){
                var a = i + 1;
                var p = i - 1;
                var aDig = parseFloat(expArray[a].trim); //value after "i"
                var bDig = parseFloat(expArray[p].trim) // value before  "i"
                var sDig = aDig - bDig;
                total = expArray.splice(bDig, 3, sDig);
            }
          }

            }
            document.form.textview.value = total;

        } ```

I am not sure exactly what you are trying to do here. isNan(numberContent) will return false & isNan(stringContent) will return true. You are using it wrongly. The 1st if should be isNaN(dig) == false & 2nd isNaN(dig) == true .


        let exp = "1/1*3";
        let expArray = exp.split(/\b/);
        console.log(expArray);   //if something doesn't work check this
        var previousValue= parseFloat(expArray[0].trim().toString());
        for(let i = 1 ; i < expArray.length; i++){
        var dig = expArray[i].trim(); //value of "i", the item currently in the loop
          if(isNaN(dig) == false){
             console.log("string")
           }
          else if(isNaN(dig) == true){
            console.log("not string");
            
              var a = i + 1;
              var aDig = previousValue
              var bDig =  parseFloat(expArray[a].trim().toString()); //value after "i"; // value before  "i"
              if (dig == "/"){ //this whole section gets skipped :(
                var sDig = aDig / bDig;
              }

              else if ( dig == "*"){
                var sDig = aDig * bDig;
              }
              else if ( dig == "+"){
                var sDig = aDig + bDig;
                
              }
              else if( dig == "-"){
                var sDig = aDig - bDig;
            }
            previousValue = sDig;
            i++
          }

            }
       alert(previousValue);

        ```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM