help correct the code so that after each entry the result is removed, and then re-activated the prompt until the abrogate



  • var res;
    
    function Calculator(){
        while(true) {
            var question = prompt('введите выражение: ', ' ');
            if (question == null) {
                break;
            }else if(question == 0){
                alert('введены некоректные данные');
            }
    
            var methods = {
                '-': function(a, b){
                    return a - b;
                },
                '+': function(a, b){
                    return a + b;
                },
                '/': function(a, b){
                    return a / b;
                },
                '*': function(a, b){
                    return a * b;
                },
                '**': function(a, b){
                    return Math.pow(a, b);
                }
            };
    
            res = question;
    
            this.calculate = function(str){
                var split = str.split(' '),
                        a = +split[0],
                        op = split[1],
                        b = +split[2];
    
                if (!methods[op] || isNaN(a) || isNaN(b)) {
                    return NaN;
                }
                alert(methods[op](+a, +b));
            }
        }
    }
    
    var calc = new Calculator();
    

    var result = calc.calculate(res);

    result();



  • I don't know how many jobs you've got, because when you bury it and get yourself and try it, nothing works.

    The cycle doesn't have to be in class, it's outside. And somehow it's a cycle to control in case.

    I did a little on my own, so there's a method called in the cycle. hasInput()♪ If he returns false♪ it ends if truecounts what was introduced. Data remain in the class field. Because if there's a class, what's the point of putting an extra variable inside the class?

    You can still modify everything. But the main idea is that the cycle should in this case be in the outer part, and in the class, it is a flag that can break it.

    function Calculator() {
      this.expression = null;
    

    this.hasInput = function() {
    return this.init();
    };

    this.init = function() {
    this.expression = prompt('введите выражение: ', ' ');
    if (this.expression == null) {
    return false;
    } else if (this.expression == 0) {
    alert('введены некоректные данные');
    }
    return true;
    };

    this.methods = {
    '-': function(a, b) {
    return a - b;
    },
    '+': function(a, b) {
    return a + b;
    },
    '/': function(a, b) {
    return a / b;
    },
    '*': function(a, b) {
    return a * b;
    },
    '**': function(a, b) {
    return Math.pow(a, b);
    }
    };

    this.calculate = function() {
    var split = this.expression.split(' '),
    a = +split[0],
    op = split[1],
    b = +split[2];

    if (!this.methods[op] || isNaN(a) || isNaN(b)) {
      return NaN;
    }
    alert(this.methods[op](+a, +b));
    

    }
    }

    var calc = new Calculator();

    while (calc.hasInput()) {
    calc.calculate();
    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2