英文:
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
...argsas parameter list forcalculate. It will only get one argument, and it should be a function. -
The
resultvariable is never set to anything else than 0. It would need to be the result by calling the callback function (the argument passed tocalculate). -
resolveshould be called with the result as argument. -
rejectshould be called withNaNas 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 tocalculate, which would lead to an error whilecalculatetries 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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论