“Js promise for calculator” 可以翻译为 “用于计算器的JavaScript Promise”。

huangapple go评论69阅读模式
英文:

Js promise for calculator

问题

以下是您要翻译的内容:

class Calculator{
    constructor(){
        this[Symbol.toStringTag] = 'Calculator'; 
    } 
    add(a, b) {
        return a + b; 
    } 
    subtract(a, b) {
        return a - b; 
    }  
    multiply(a, b) {
        return a * b;
    } 
    divide(a, b) {
        if(b === 0){ 
            return NaN;
        }
        return a / b; 
    }
    toString(){
        return "Calculator";
    }
    calculate(...args) {
        var result = 0;
        return new Promise(function(resolve, reject){
            setTimeout(function() {
                if(result === NaN) {
                   reject(NaN);
                } else {
                   resolve(result);
                }
            }, 1000);
        });
    }
}

以下是您要翻译的测试部分:

describe( "Calculator.calculate", function(){
  let calculator;

  beforeEach( function(){
    calculator = new Calculator();
  } );
  
  it( "returns a promise", function( done ){
    const
      callDone = () => done(),
      calculating = calculator.calculate( () => void 0 );
    expect( calculating ).to.be.instanceOf( Promise );
    calculating.then( callDone ).catch( callDone );
  } );
     
  it( "resolves with the result when the calculation succeeds", function( done ){
    const calculating = calculator.calculate( function(){
      expect( this ).to.equal( calculator );
      let result = 0;
      result += this.add( 1, 2 );
      result += this.add( 3, 4 );
      return result;
    } );
    calculating
      .then( function( result ){
        expect( result ).to.equal( 10 );
        done();
      } )
      .catch( () => done() );
  } );
  
  it( "rejects with NaN when the calculation fails", function( done ){
    const calculating = calculator.calculate();
    calculating.catch( function( result ){
      expect( result ).to.be.NaN;
      done();
    } );
  } );
} );

请注意,我已将其中的HTML实体(例如")转换为普通文本,以便更清晰地阅读。如果您需要进一步的帮助,请随时提出。

英文:

I have a simple calculator function that I wrote for a coding challenge. Now I'm having trouble with an additional calculate function that should return a promise.

class Calculator{
    constructor(){
        this[Symbol.toStringTag] = 'Calculator'; 
    } 
    add(a, b) {
        return a + b; 
    } 
    subtract(a, b) {
        return a - b; 
    }  
    multiply(a, b) {
        return a * b;
    } 
    divide(a, b) {
        if(b === 0){ 
            return NaN;
        }
        return a / b; 
    }
    toString(){
        return "Calculator";
    }
    calculate(...args) {
        var result = 0;
        return new Promise(function(resolve, reject){
            setTimeout(function() {
                if(result === NaN) {
                   reject(NaN);
                } else {
                   resolve(result);
                }
            }, 1000);
        });
    }
}

And here are the tests that the promise needs to meet:

describe( "Calculator.calculate", function(){
  let calculator;

  beforeEach( function(){
    calculator = new Calculator();
  } );
  
  it( "returns a promise", function( done ){
    const
      callDone = () => done(),
      calculating = calculator.calculate( () => void 0 );
    expect( calculating ).to.be.instanceOf( Promise );
    calculating.then( callDone ).catch( callDone );
  } );
     
  it( "resolves with the result when the calculation succeeds", function( done ){
    const calculating = calculator.calculate( function(){
      expect( this ).to.equal( calculator );
      let result = 0;
      result += this.add( 1, 2 );
      result += this.add( 3, 4 );
      return result;
    } );
    calculating
      .then( function( result ){
        expect( result ).to.equal( 10 );
        done();
      } )
      .catch( () => done() );
  } );
  
  it( "rejects with NaN when the calculation fails", function( done ){
    const calculating = calculator.calculate();
    calculating.catch( function( result ){
      expect( result ).to.be.NaN;
      done();
    } );
  } );
} );

The above calculate function I wrote only passes the first test and none of the other. I fear I'm going about it all wrong. How can I make it work?

答案1

得分: 0

以下是要翻译的内容:

  • 需求没有暗示您需要使用 setTimeout延迟 结果。

  • 对于 calculate 方法,不需要使用 ...args 作为参数列表。它只会接收一个参数,而且应该是一个函数。

  • result 变量永远不会被赋值为除了 0 之外的任何值。它应该是调用回调函数(传递给 calculate 的参数)的结果。

  • 应该使用结果调用 resolve

  • 应该使用 NaN 作为参数调用 reject

  • 没有规定当结果为 NaN 时,Promise 应该被拒绝。相反,情况是相反的:当Promise拒绝(因为发生错误)时,它应该以 NaN 作为原因拒绝。测试序列中的最后一个测试未传递任何参数给 calculate,这将导致 calculate 尝试执行未定义的参数而引发错误。然后它应该返回一个被拒绝的Promise。

我们可以通过将方法声明为 async 来避免调用Promise构造函数。以下是符合这些测试的实现:

    async calculate(f) {
        try {   
            return f.call(this);
        } catch {
            throw NaN;
        }
    }
英文:

Some remarks on your attempt:

  • The requirements do not suggest that you need to delay a result using setTimeout.

  • There is no need for ...args as parameter list for calculate. It will only get one argument, and it should be a function.

  • The result variable is never set to anything else than 0. It would need to be the result by calling the callback function (the argument passed to calculate).

  • resolve should be called with the result as argument.

  • reject should be called with NaN as argument

  • There is no specification that when the result is NaN, the promise should be rejected. The opposite is true: when the promise rejects (because of an error) it should reject with NaN as reason. The last test in the test sequence, passes no argument to calculate, which would lead to an error while calculate tries to execute an argument that is undefined. And then it should return a rejected promise.

We can avoid calling the Promise constructor by declaring the method as async. Here is an implementation that passes those tests:

    async calculate(f) {
        try {   
            return f.call(this);
        } catch {
            throw NaN;
        }
    }

huangapple
  • 本文由 发表于 2023年6月8日 04:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定