📖 Scenario: Imagine you have a list of fruits and you want to say hello to each fruit one by one. Doing this without loops means writing the same message many times. Loops help us repeat actions easily.
🎯 Goal: You will create a list of fruits, set up a counter, use a loop to greet each fruit, and finally print all greetings.
📋 What You'll Learn
Create an array called fruits with these exact values: "Apple", "Banana", "Cherry"
Create a variable called greetings and set it to an empty array
Use a for loop with variable i to go through fruits
Inside the loop, add a greeting string `Hello, ${fruits[i]}!` to greetings
Print the greetings array
💡 Why This Matters
🌍 Real World
Loops are used in many apps to process lists of items, like showing messages for each user or calculating totals.
💼 Career
Understanding loops is essential for any programming job because they make code efficient and easier to maintain.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact values: "Apple", "Banana", "Cherry"
Javascript
Hint
Use square brackets [] to create an array and separate items with commas.
2
Create an empty greetings array
Create a variable called greetings and set it to an empty array []
Javascript
Hint
An empty array is created with [].
3
Use a for loop to add greetings
Use a for loop with variable i to go through fruits. Inside the loop, add a greeting string `Hello, ${fruits[i]}!` to the greetings array using push()
Javascript
Hint
Use fruits.length to get the number of fruits and push() to add items to the array.
4
Print the greetings array
Write console.log(greetings) to print the greetings array
Javascript
Hint
Use console.log() to show the array in the console.
Practice
(1/5)
1. Why do we use loops in JavaScript? for or while loops help us to:
easy
A. Repeat a task multiple times without writing it again
B. Write code only once and never run it
C. Make the program run slower
D. Stop the program from running
Solution
Step 1: Understand the purpose of loops
Loops allow repeating the same action many times without rewriting the code.
Step 2: Compare options with loop purpose
Only Repeat a task multiple times without writing it again matches this purpose by saying loops repeat tasks multiple times.
Final Answer:
Repeat a task multiple times without writing it again -> Option A
Quick Check:
Loops repeat tasks = B [OK]
Hint: Loops repeat code to save writing it many times [OK]
Common Mistakes:
Thinking loops slow down the program
Believing loops stop the program
Confusing loops with one-time code
2. Which of these is the correct syntax to start a for loop in JavaScript?
easy
A. for (let i = 0; i < 5; i++) { }
B. for i in range(5) { }
C. loop (i = 0; i < 5; i++) { }
D. for i = 0 to 5 { }
Solution
Step 1: Recall JavaScript for loop syntax
The correct syntax uses parentheses with initialization, condition, and increment separated by semicolons.
Step 2: Match options to syntax
for (let i = 0; i < 5; i++) { } matches this exactly: for (let i = 0; i < 5; i++) { }.
Final Answer:
for (let i = 0; i < 5; i++) { } -> Option A
Quick Check:
JS for loop syntax = D [OK]
Hint: JS for loops use semicolons inside parentheses [OK]
Common Mistakes:
Using Python or other language syntax
Missing parentheses or semicolons
Using wrong keywords like 'loop'
3. What will this code print?
for (let i = 1; i <= 3; i++) {
console.log(i);
}
medium
A. 0 1 2
B. 1 2 3
C. 1 2 3 4
D. 3 2 1
Solution
Step 1: Analyze loop start and end values
The loop starts at i=1 and runs while i is less than or equal to 3.
Step 2: List printed values
It prints 1, then 2, then 3, then stops because i becomes 4.
Final Answer:
1 2 3 -> Option B
Quick Check:
Loop prints 1 to 3 = C [OK]
Hint: Check loop start, end, and condition carefully [OK]
Common Mistakes:
Starting count from 0 instead of 1
Including number 4 by mistake
Printing in reverse order
4. Find the error in this loop:
for (let i = 0; i < 5; i++)
console.log(i)
}
medium
A. Missing opening brace { after for statement
B. Incorrect loop condition
C. Variable i is not declared
D. Extra closing brace } without matching {
Solution
Step 1: Check braces in the code
The loop has no opening brace { but has a closing brace } at the end.
Step 2: Identify mismatch
Extra closing brace } without a matching opening brace causes syntax error.
Final Answer:
Extra closing brace } without matching { -> Option D
Quick Check:
Brace mismatch error = A [OK]
Hint: Count opening and closing braces carefully [OK]
Common Mistakes:
Forgetting to add opening brace {
Assuming missing semicolon causes error here
Not noticing extra closing brace
5. You want to print all even numbers from 2 to 10 using a loop. Which code correctly does this?
hard
A. for (let i = 2; i < 10; i++) { console.log(i); }
B. for (let i = 0; i <= 10; i++) { if (i % 2 === 0) console.log(i); }
C. for (let i = 2; i <= 10; i += 2) { console.log(i); }
D. for (let i = 0; i <= 10; i += 3) { console.log(i); }
Solution
Step 1: Understand the goal
We want to print even numbers from 2 to 10 inclusive.
Step 2: Check each option
for (let i = 2; i <= 10; i += 2) { console.log(i); } starts at 2, increments by 2, and stops at 10, printing 2,4,6,8,10 correctly.
Final Answer:
for (let i = 2; i <= 10; i += 2) { console.log(i); } -> Option C
Quick Check:
Increment by 2 from 2 to 10 = A [OK]
Hint: Use step of 2 to print even numbers directly [OK]
Common Mistakes:
Printing odd numbers by mistake
Stopping before 10
Using wrong increment step
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.