Exercise 1.6: Array Loops

Level 1

Exercise 1: forEach: You have an array of names. Use forEach to print each name to the console: let names = ['Anna', 'Bernat', 'Clara'];

Exercise 2: for-of: You have an array of names. Use a for-of loop to print each name to the console: let names = ['Anna', 'Bernat', 'Clara'];

Exercise 3: filter: You have an array of numbers. Use filter to create a new array containing only the even numbers. let numbers = [1, 2, 3, 4, 5, 6];

Level 2

Exercise 4: for-in: You have an object with key-value pairs: let obj = { name: 'Ona', age: 25, city: 'Barcelona' }; Use a for-in loop to print each key and its corresponding value to the console.

Exercise 5: for-of with break: You have an array of numbers. Use a for-of loop to print numbers to the console until you find the number 5, then stop the loop: let numbers = [1, 2, 3, 4, 5, 6];

Level 3

Exercise 6: for-of with index: Use a for-of loop to print each element of the array and its position (index) to the console: let names = ['Anna', 'Bernat', 'Clara']

⚠️ Important: Open the browser console (F12 or Ctrl+Shift+I) to see the results of the exercises and verify that they work correctly.

Back to index