Javascript Odin项目-基础4练习-sumAll

Javascript Odin项目-基础4练习-sumAll,javascript,arrays,sum,Javascript,Arrays,Sum,我被困在奥丁项目基础4部分的sumAll练习中。我设法通过了测试,我需要的结果是“错误”。但是,我无法找出正确的代码来通过其他测试。我哪里出错了 以下是练习: const sumAll = require(’./sumAll’) describe(‘sumAll’, function() { it(‘sums numbers within the range’, function() { expect(sumAll(1, 4)).toEqual(10); }); it(‘works with

我被困在奥丁项目基础4部分的sumAll练习中。我设法通过了测试,我需要的结果是“错误”。但是,我无法找出正确的代码来通过其他测试。我哪里出错了

以下是练习:

const sumAll = require(’./sumAll’)

describe(‘sumAll’, function() {
it(‘sums numbers within the range’, function() {
expect(sumAll(1, 4)).toEqual(10);
});
it(‘works with large numbers’, function() {
expect(sumAll(1, 4000)).toEqual(8002000);
});
it(‘works with larger number first’, function() {
expect(sumAll(123, 1)).toEqual(7626);
});
it(‘returns ERROR with negative numbers’, function() {
expect(sumAll(-10, 4)).toEqual(‘ERROR’);
});
it(‘returns ERROR with non-number parameters’, function() {
expect(sumAll(10, “90”)).toEqual(‘ERROR’);
});
it(‘returns ERROR with non-number parameters’, function() {
expect(sumAll(10, [90, 1])).toEqual(‘ERROR’);
});
});
我的代码:

const sumAll = function(a, b) {
const arr = [];
if (a < b) {
while (a <= b) {
arr.push(a++);
}
} else if (b < a) {
while (b <= a) {
arr.push(b++);
}
} else {
arr.push(a);
}

if (a < 0 || b < 0) {
return “ERROR”;
} else if (typeof a !== NaN || typeof b !== NaN) {
return “ERROR”;
}
return arr.reduce((a, b) => a + b);
}

module.exports = sumAll
const sumAll=函数(a,b){
常数arr=[];
if(a
const sumAll = function (x, y) {
if (x > 0 && y > 0 && typeof x === 'number' && typeof y === 'number') {
    var valorx = x;
    var valory = y;
    var total = 0;
    if (x < y) {
        for (var i = valorx; i <= valory; i++) {
            total += i;
        }
        return total;
    } else if (x > y) {
        for (var i = valory; i <= valorx; i++) {
            total += i;
        }
        return total;
    }
} else {
    return 'ERROR'
 }
}

module.exports = sumAll
const sumAll=函数(x,y){
如果(x>0&&y>0&&typeof x=='number'&&typeof y=='number'){
var valorx=x;
var valory=y;
var合计=0;
if(x对于(var i=valory;我请不要仅将代码作为答案发布,还要解释代码的作用以及它如何解决问题。带有解释的答案通常更有帮助,质量更好,并且更有可能吸引更多的选票。