"learn from WeChat Mini Programs Development from scratch"-- Gao Hongtao wrote a calculator

// pages/cal/cal.js
var calculate = function (data1, oper, data2) {
  var data;
  data1 = parseFloat(data1);
  data2 = parseFloat(data2);
  switch (oper) {
    case "+":
      data = data1 + data2;
      break;
    case "-":
      data = data1 - data2;
      break;
    case "*":
      data = data1 * data2;
      break;
    case "/":
      if (data2 != 0) {
        data = data1 / data2;
      }
      else {
        data = 0;
      }
      break;
  }
}
Page({

  /**
   * 
   */
  data: {
    temp: "0",
    lastoper: "+",
    falg: true,
    result:"0",
    id1:"history",
    id2:"clear",
    id3:"back",
    id4:"div",
    id5:"num_7",
    id6:"num_8",
    id7:"num_9",
    id8:"mul",
    id9:"num_4",
    id10:"num_5",
    id11:"num_6",
    id12:"sub",
    id13:"num_1",
    id14:"num_2",
    id15:"num_3",
    id16:"add",
    id17:"negative",
    id18:"num_0",
    id19:"dot",
    id20:"equ"
  },
clickButton:function(e){
  var data=this.data.result;
  var tmp=this.data.temp;
  var lastoper1=this.data.lastoper;
  var noNumFlag=this.data.flag;
  
  //console.log(this.data.result);
  if(e.target.id>="num_0" && e.target.id<="num_9"){
    data+=e.target.id.split("_")[1];
    if(this.data.result=="0" || noNumFlag){
      data=e.target.id.split("_")[1];
    }
    noNumFlag=false;
  }
  else
  {
    noNumFlag = true;
    console.log(e.target.id);
    if(e.target.id=="dot")
    {
      if(data.toString().indexOf(".")==-1)
      {
        data+=".";
      }
      noNumFlag = false;
    }
    else if(e.target.id=="clear")
    {
      data=0;
      tmp=0;
      lastoper1="+";
    }
    else if(e.target.id=="negative")
    {
      data=-1*data;   
    }
    else if(e.target.id=="back")
    {
      if(data.toString().length>1)
      {
        data = data.substr(0, data.toString().length-1);
      }
      else
      {
        data=0;
      }
      
    }
    else if(e.target.id=="div")
    {
      data=calculate(tmp,lastoper1,data);
      tmp=data;
      lastoper1="/";
    }
    else if(e.target.id=="mul")
    {
      data=calculate(tmp,lastoper1,data);
      tmp=data;
      lastoper1="*";
    }
    else if (e.target.id == "add") 
    {
      data = calculate(tmp, lastoper1, data);
      tmp = data;
      lastoper1 = "+";
    }
    else if (e.target.id == "sub") 
    {
      data = calculate(tmp, lastoper1, data);
      tmp = data;
      lastoper1 = "-";
    }
    else if (e.target.id == "equ") {
      data = calculate(tmp, lastoper1, data);
      tmp = 0;
      lastoper1 = "+";
    }
  }
  this.setData({
    result: data,
    lastoper: lastoper1,
    temp: tmp,
    flag: noNumFlag
  });
}
})

call the function written above and report an error?

Mar.03,2021

I wrote the wrong code and solved


what's wrong? I'm in the same situation as you.


Hello, mine is also wrong! Excuse me, what's wrong with you?


   else if(e.target.id=="mul")
    {
      data=calculate(tmp,lastoper1,data);
      tmp=data;
      lastoper1="*";
    }

is the execution order of this code from bottom to top? if not when you call calculate, tmp should be 0 lastoper should be +

Menu