Tag:i++ try log parse Operations als calculator index ring
var calc={getdata:function (arr) {var tempstr = ""; var data = [];//formatted data for (var i = 0; i < arr.length; i++) {if (number (arr[i)) | | Number (Arr[i]) = = 0) {TempStr + = Arr[i]}else{if (tempstr! = ") {Data.push (TEMPSTR);} Data.push (Arr[i]); tempstr = ";}} if (tempstr! = ") {Data.push (TEMPSTR); tempstr = ';} Remove 0 start for (var i = 0; i < data.length; i++) {if (number (Data[i]) | | Number (Data[i]) = = 0) {Data[i] = number (Data[i])}}if (data[data.length-1] = = "=") {var result = Calc.calute (data); Data.push (result);} Calc.show (data),},show:function (arr) {Console.log (Arr.join (")");},calute:function (arr) {var newArr = Json.parse ( Json.stringify (arr));//Radical while (newarr.indexof (' sqrt ')! =-1) {var index = newarr.indexof (' sqrt '); var num1 = newarr[ Index+1];var result = Math.sqrt (NUM1); Newarr.splice (Index,2,result);} Compute Division while (Newarr.indexof ('/')! =-1) {var index = newarr.indexof ('/'); var num1 = Newarr[index-1];var num2 = Newarr[index +1];var result = Num1/num2;newarr.splice (Index-1,3,result);} Multiplication while (newarr.indexof (' * ')! =-1) {var index = newarr.indexof (' * '); var num1 = Newarr[index-1];var num2 = Newarr[index+1];var result = Num1*num2;newarr.sp Lice (Index-1,3,result);} Subtraction while (Newarr.indexof ('-')! =-1) {var index = newarr.indexof ('-'); var num1 = Newarr[index-1];var num2 = newarr[index+1 ];var result = Num1-num2;newarr.splice (Index-1,3,result);} Addition while (Newarr.indexof (' + ')! =-1) {var index = newarr.indexof (' + '); var num1 = Newarr[index-1];var num2 = newarr[index+1 ];var result = Num1+num2;newarr.splice (Index-1,3,result);} Try{if (Newarr.length > 2) {throw new error ("Calculation error");}} catch (E) {console.log (e); Console.log (NEWARR); return "Expression error";} return newarr[0];}} var arr = ["0", "2", "0", "+", "3", "+", "sqrt", "4", "-", "8", "="]; Calc.getdata (arr);//type:1: null, 2: number, 3: operator, 4: equals function Vaild (oldtype,type) {var flag = False;//type = 2 o'clock if (Oldtype = = 1 && type = = 2) | | (Oldtype = = 2 && type = = 2) | | (Oldtype = = 3 && type = = 2)) {return true;} Type = = 3if (Oldtype = = 2 && type = = 3) {flag = true;} TypE = = 4if (Oldtype = = 2 && type = = 4) {flag = true;} return flag;}
JS Simple calculator Bottom arithmetic logic