Codeloft
0 0 XP

Arrays and loops

8 min 20 XP + 10 for no hints

An array stores a list of values in one variable: let scores = [12, 7, 20];. Each item has a position called an index, starting at 0, so scores[0] is 12. scores.length tells you how many items there are.

A for...of loop runs once for each item: for (const s of scores) { … }. Inside the loop, s is the current item.

A common pattern is the running total: start at 0, then add each item inside the loop.

Example
let prices = [4, 10, 6];
let total = 0;

for (const price of prices) {
  total = total + price;
}

console.log("Items:", prices.length);
console.log("Total:", total);
total += n is a short way to write total = total + n.

Your tasks

0 of 3 done
  1. sum([1, 2, 3]) returns 6Expected 6, got undefined
  2. sum([]) returns 0Expected 0, got undefined
  3. countAbove([5, 12, 8, 20], 9) returns 2Expected 2, got undefined

Quick check

What is ["a", "b", "c"][2]?

scores.jsPress Run or Ctrl + Enter
undefined