I'm trying to make my small function work which adds every number together in a range.
For example when I call the method like: sumAll(3,10) it should do
3+4+5+6+7+8+9+10
It works if I give the function positive integers but if it receives a negative number or a string or an array for example, it doesn't work properly.. I just want to return "ERROR" if the supplied parameter is not a positive integer.
Can I have some help with this please? Is there a more elegant (better) way?
My code:
const sumAll = (...args) => {
let max = Math.max(...args);
let min = Math.min(...args);
if ((min < 0) || (!Number.isInteger(min)) || (!Number.isInteger(max)) || (Array.isArray(...args))) {
return "ERROR";
}
let n = (max - min) + 1;
return ((max + min) * n) / 2;
}