-
Notifications
You must be signed in to change notification settings - Fork 70
/
forEachLoops.js
28 lines (15 loc) · 955 Bytes
/
forEachLoops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
let array = [1, 2, 3, 4, 5];
// 1. Use a forEach loop to print each element to the console.
// 2. Create an array of colors ['red', 'green', 'blue'].
// - Use a forEach loop to add "Color:" before each element and print them to the console.
// - The output should look like "Color: red", "Color: green", "Color: blue".
let numbers = [1, 2, 3, 4, 5];
let sum = 0;
// 3. Use a forEach loop to calculate and print the sum of all the elements in the array.
// - There's a couple ways to solve this one - think through the logic and ask for support if you need it :)
let fruits = ["apple", "banana", "cherry"]
// 4. Use a forEach loop to print each element to the console.
let names = ["Simba", "Scar", "Nala", "Mufasa", "Zazu"];
// 5. Use a forEach loop to print each name along with its index number.
// - Remember: the index can be put as a second parameter.
// - Example: the first message should read something like "Simba (Index: 0)"