JavaScript Intermediate
Live preview
Array methods: map, filter and reduce
Transform, select and total a list of quiz scores without a single for loop.
What you will learn
- Create a new array with map()
- Keep only some items with filter()
- Combine items into one value with reduce()
Run the preview: the console shows what each line produces.
const scores = [45, 72, 88, 39, 91, 66];
// map: a new array, one result per item
const percentages = scores.map(score => score + '%');
console.log('As percentages:', percentages);
// filter: only the items that pass a test
const passed = scores.filter(score => score >= 50);
console.log('Passed (50 and above):', passed);
// reduce: combine everything into one value
const total = scores.reduce((sum, score) => sum + score, 0);
console.log('Average score:', (total / scores.length).toFixed(1));
Run the preview when you are readyIt runs in an isolated sandbox with no access to this site or your account.
Make it your ownOpen the HTML, CSS and JavaScript in the Playground, change anything, and see the result instantly.
Edit in Playground