Skip to the content

map and filter

JavaScript · Unit 5: Arrays

map creates a new array: you hand it a function and it stores whatever that function returns with each item.

const nums = [10, 20, 30];
const doubles = nums.map(
  (n) => n * 2
);
console.log(doubles);

Prints

[ 20, 40, 60 ]

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Complete the code

    Complete it to turn the pounds into ounces.

  3. 3. Multiple choice

    Which line does the same as this loop?

  4. 4. Predict the output

    What does this code print?

  5. 5. Predict the output

    What does this code print?

  6. 6. Complete the code

    Complete it to keep the failing scores (under 60).

  7. 7. Multiple choice

    You have the prices and you want the ones over 100, without changing anything. What do you use?

  8. 8. Find the bug

    This program should print the doubles. Which line has the error?

  9. 9. Put the lines in order

    Put in order the function that returns only the passing scores (60 or more).

  10. 10. Predict the output

    These are Mia's scores. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →