Lists With Arrays (Summary)

When you started this course, every variable held one value. Now one variable can name a whole list, and you can write functions that answer real questions about it, such as how many snacks are on a shared camping list, how long you walked this week, or which crate is too heavy to carry.

This last lesson pulls those skills together. First, you’ll recall them with one short script. Then you’ll get a compact overview to keep and a look at where lists go in the next course.

Predict Before You Look Back

You might nod along as you read a list of what you learned. Recalling it yourself helps it stick, so start with a prediction. Back at movie night, say you have a list of snack prices in dollars and you add one more snack. Create review.js in your course folder with this script, but don’t run it yet.

Language: JavaScript Filename: review.js
function countAtLeast(prices, limit) {
  let count = 0;
  for (const price of prices) {
    if (price >= limit) {
      count = count + 1;
    }
  }
  return count;
}

const prices = [4, 2, 7, 2];
prices.push(5);
console.log(prices.length);
console.log(prices[1]);
console.log(prices.at(-1));
console.log(countAtLeast(prices, 4));
console.log(countAtLeast([], 4));

Write down the five lines you expect. Then answer two more questions in a sentence each. Why does count start at 0? And why is for...of enough here, when a counting loop would also work? When you have your answers, run the script.

Language: Shell
$ node review.js
5
2
5
3
0

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.