8000 Update sumAll-solution.js · ewoknock/javascript-exercises@5513be5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5513be5

Browse files
authored
Update sumAll-solution.js
1 parent 3ecdab9 commit 5513be5

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

05_sumAll/solution/sumAll-solution.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
const sumAll = function (min, max) {
22
if (!Number.isInteger(min) || !Number.isInteger(max)) return "ERROR";
33
if (min < 0 || max < 0) return "ERROR";
4-
if (min > max) [min, max] = [max, min];
4+
if (min > max) {
5+
const temp = min;
6+
min = max;
7+
max = temp;
8+
}
59

10+
// An alternative way to swap the values of min and max like above is to use the array destructuring syntax.
11+
// Here's an optional article on it: https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/
12+
// if (min > max) [min, max] = [max, min];
13+
614
let sum = 0;
715
for (let i = min; i <= max; i++) {
816
sum += i;

0 commit comments

Comments
 (0)
0