js计算器小练习

2015-11-28  本文已影响130人  大白好

function  calculator(){

        var  operator =arguments[arguments.length - 1];

        if ( operator == '+'){

              for (var i = 1, i<arguments.leng - 1; i++) {

                       arguments[0] +=arguments[i];

              };

               console.log(arguments[0]);

        }else if ( operator == '-'){

                for (var i = 1, i <arguments.length - 1; i++){

                        arguments[0] -=arguments[i];

                 };

                  console.log(arguments[0]);

        } else if ( operator == '*'){

                  for (var i = 1, i<arguments.length -  1;i++){

                          arguments[0] *=arguments[i];

                   };

                  console.log(arguments[0]);

        } else if ( operator == '/'){

                  for (var i =1,i<arguments.length -1;i++){

                          arguments[0]/=arguments[i];

                  };

                  console.log(arguments[0]);

       }

}

calculator(1,2,3,4,'+');

calculator(80,20,13,12,'-');

calculator(1,2,3,4,5,'*');

calculator(100,2,5,2,'/');

                  

                  

                          

        

上一篇下一篇

猜你喜欢

热点阅读