I'm wondering how the following looks for a javascript Array-sum function:
function sum(...rest) {
let n = 0;
for (let elem of rest) {
n += elem;
}
return n;
}
console.log(sum(1,2,3));
console.log(sum(2));
Is this considered overly verbose code? For example, of these examples are much more 'interesting' and generally speaking, shorter: https://stackoverflow.com/a/43363105/651174. In the above, what could be improved?
1 Answer 1
Reduce
You could have used Array.reduce
function sum(...rest) {
return rest.reduce((n, val) => n + val, 0);
}
Or as
const sum = (...rest) => rest.reduce((n, val) => n + val, 0);
Your code
But there are many ways to do the same thing and there is no best way to do it.
There is nothing wrong with your code and looking at your code I would do things slightly differently but mostly these are subjective style considerations to fit a larger body of work.
Change the naming
- The Function name
sum
gives no clue to what it does, changing tosumArray
orsumValues
will be much more readable if found on its own. rest
tovalues
ornumbers
n
tosum
elem
toval
oritem
eg
function sum(...values) {
let sum = 0;
for (let val of values) {
sum += val;
}
return sum;
}
Layout and declarations
I would make sum
a var
(more fitting of its use), val
a const
, and put the addition on the same line as the loop.
eg
function sumValues(...values) {
var sum = 0;
for (const val of values) { sum += val }
return sum;
}
Personalty the arrow function would be my preferred solution incorporating the name changes to give a simple one liner.
const sumValues = (...values) => values.reduce((sum, val) => sum + val, 0);
-
\$\begingroup\$ thanks for all the suggestions! One question, for
var sum = 0;
I thought that in newer js one was never to usevar
, but onlylet
orconst
. Could you explain why it would bevar
here instead oflet
? \$\endgroup\$David542– David5422022年04月21日 06:20:36 +00:00Commented Apr 21, 2022 at 6:20 -
1\$\begingroup\$ @David542
var
because the variable is scoped to the function. It is much cleaner to place a single declaration of function scoped variable/s in one place at the top of the function. The block scopedlet
declaration is noisy and appears randomly within the function (often the same name declared multiple times). If you keep your functions small and simple there is no benefit at all to block scoped declarations. With the exception of constantsconst
which should be the preferred declaration, and by their nature need to be declared as needed within the function. \$\endgroup\$Blindman67– Blindman672022年04月21日 06:50:11 +00:00Commented Apr 21, 2022 at 6:50 -
\$\begingroup\$ The Function name sum gives no clue to what it does, changing to sumArray or sumValues... => sumArrayElementValuesAsOperands would be clearer still. But still, it does not tell us what "sum" does. We can only infer sumthing is done to an array or maybe with an array, but that's not clear either. Oh, this observation assumes there's no typo of some kind; but still, it could have to do with "Spaceman Spiff has array gun" or space cadets generally. \$\endgroup\$radarbob– radarbob2022年04月21日 23:16:17 +00:00Commented Apr 21, 2022 at 23:16
-
\$\begingroup\$ @radarbob The reason I pointed out the function name is to draw attention to the naming. Without context or abstraction the alternative name
sumValues
is an example, how, rather than what. Eg verb "sum" acts on "values". \$\endgroup\$Blindman67– Blindman672022年04月22日 00:54:04 +00:00Commented Apr 22, 2022 at 0:54 -
\$\begingroup\$ No context - yep. A containing object tends to supply that context. But that's a little different from "what it does." Simple comment like "takes an Array of integers" does the trick. "sumArray" doesn't tell me about the calling parameters, if any. \$\endgroup\$radarbob– radarbob2022年04月22日 03:43:36 +00:00Commented Apr 22, 2022 at 3:43