Update sumAll-solution.js

This commit is contained in:
Will 2023-07-29 15:39:33 -05:00 committed by GitHub
parent 3ecdab9531
commit 5513be576a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 1 deletions

View File

@ -1,7 +1,15 @@
const sumAll = function (min, max) {
if (!Number.isInteger(min) || !Number.isInteger(max)) return "ERROR";
if (min < 0 || max < 0) return "ERROR";
if (min > max) [min, max] = [max, min];
if (min > max) {
const temp = min;
min = max;
max = temp;
}
// An alternative way to swap the values of min and max like above is to use the array destructuring syntax.
// Here's an optional article on it: https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/
// if (min > max) [min, max] = [max, min];
let sum = 0;
for (let i = min; i <= max; i++) {