I’m stuck in the sumAll exercise from the Fundamentals portion of the Odin Project. I passed the test in which I need the result to be ‘ERROR’. However, I cannot figure out the correct code to pass the other tests. Where did I go wrong?
This is the exercise:
const sumAll = require('./sumAll')
describe('sumAll', () => {
test('sums numbers within the range', () => {
expect(sumAll(1, 4)).toEqual(10);
});
test('works with large numbers', () => {
expect(sumAll(1, 4000)).toEqual(8002000);
});
test('works with larger number first', () => {
expect(sumAll(123, 1)).toEqual(7626);
});
test('returns ERROR with negative numbers', () => {
expect(sumAll(-10, 4)).toEqual('ERROR');
});
test('returns ERROR with non-number parameters', () => {
expect(sumAll(10, "90")).toEqual('ERROR');
});
test('returns ERROR with non-number parameters', () => {
expect(sumAll(10, [90, 1])).toEqual('ERROR');
});
});
My code:
const sumAll = function(...args) {
if(!isNumeric(args) || !checkPositive(args)){
return "ERROR";
}
else{
args.sort();
let result=0;
for(let i=args[0]; i<=args[1]; i++){
result += i;
}
return result;
}
};
//check for +ve numbers
function checkPositive(arr){
return arr.forEach(element =>{
if(element>0){
return true;
}
else{
return false;}
});
}
//check for numeric values
function isNumeric(arr){
return arr.forEach(element =>{
if(typeof element == "number"){
return true;
}
else{
return false;}
});
}
module.exports = sumAll;
enter code here