Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
JAVASCRIPT DATA / CHAPTER 02 / Learn / LESSON 04
Fourth class. From a list we build a sum and an average — budget classics.
function sumAmounts(list) { let total = 0; for (const n of list) total += n; return total; }
function averageAmount(list) { if (list.length === 0) return 0; return sumAmounts(list) / list.length; }
Empty list: average is 0 (avoid dividing by zero).
Common mistake: forgetting [] → 0; or computing total without return.
Accumulate in a loop; guard against divide-by-zero.
DATA STANDBY
›