📖 Scenario: Imagine you have a list of your favorite fruits in order. Sometimes you want to see them in reverse order, like reading a list from bottom to top.
🎯 Goal: You will create an array of fruits, set up a helper variable for the reversed array, reverse the array using a loop, and finally print the reversed list.
📋 What You'll Learn
Create an array called fruits with these exact values: 'apple', 'banana', 'cherry', 'date', 'elderberry'
Create an empty array called reversedFruits
Use a for loop with variable i starting from the last index of fruits down to 0
Inside the loop, add fruits[i] to reversedFruits
Print the reversedFruits array
💡 Why This Matters
🌍 Real World
Reversing arrays is useful when you want to display data in the opposite order, like showing recent messages first or undoing a list.
💼 Career
Understanding array manipulation and loops is fundamental for programming jobs, especially when working with data or user interfaces.
Progress0 / 4 steps
1
Create the fruits array
Create an array called fruits with these exact values: 'apple', 'banana', 'cherry', 'date', 'elderberry'
DSA Javascript
Hint
Use square brackets [] to create an array and separate items with commas.
2
Create an empty array for reversed fruits
Create an empty array called reversedFruits
DSA Javascript
Hint
Use [] to create an empty array and assign it to reversedFruits.
3
Reverse the array using a for loop
Use a for loop with variable i starting from fruits.length - 1 down to 0. Inside the loop, add fruits[i] to reversedFruits using push
DSA Javascript
Hint
Start i at fruits.length - 1 and decrease it until 0. Use push to add items to reversedFruits.
4
Print the reversed array
Print the reversedFruits array using console.log
DSA Javascript
Hint
Use console.log(reversedFruits) to show the reversed array.
Practice
(1/5)
1. What does the reverse() method do to an array in JavaScript?
easy
A. It removes duplicate elements from the array.
B. It sorts the array in ascending order.
C. It reverses the order of elements in the original array.
D. It creates a new array with elements doubled.
Solution
Step 1: Understand the reverse() method
The reverse() method changes the original array by reversing the order of its elements.
Step 2: Confirm the effect on the original array
It does not create a new array or sort; it simply reverses the existing array in place.
Final Answer:
It reverses the order of elements in the original array. -> Option C
Quick Check:
reverse() = reverses array order [OK]
Hint: reverse() flips array order in place [OK]
Common Mistakes:
Thinking reverse() sorts the array
Assuming reverse() creates a new array
Confusing reverse() with removing duplicates
2. Which of the following is the correct syntax to reverse an array named arr in JavaScript?
easy
A. arr.reverse();
B. reverse(arr);
C. arr = reverse();
D. arr.reverse(arr);
Solution
Step 1: Identify the method call syntax
The reverse() method is called on the array object itself, so the syntax is arr.reverse();.
Step 2: Check other options for errors
Options A, B, and C are incorrect because reverse is not a standalone function and does not take arguments.
Final Answer:
arr.reverse(); -> Option A
Quick Check:
Method called on array object = arr.reverse() [OK]
Hint: Use arrayName.reverse() to reverse in place [OK]
The reverse() method reverses the original array and returns the same reversed array.
Step 2: Trace the code output
After calling arr.reverse(), arr becomes [4, 3, 2, 1], and reversed holds this reversed array. Logging reversed prints [4, 3, 2, 1].
Final Answer:
[4, 3, 2, 1] -> Option D
Quick Check:
reverse() returns reversed array [OK]
Hint: reverse() returns the reversed array itself [OK]
Common Mistakes:
Expecting original array unchanged
Thinking reverse() returns undefined
Confusing reverse() with sorting
4. Find the error in the following code that tries to reverse an array nums:
function reverseArray(nums) {
for(let i = 0; i < nums.length / 2; i++) {
let temp = nums[i];
nums[i] = nums[nums.length - i];
nums[nums.length - i] = temp;
}
return nums;
}
medium
A. The index nums.length - i is out of bounds; should be nums.length - i - 1.
B. The temp variable is not needed for swapping.
C. The loop should run while i < nums.length / 2, not <=.
D. The function should return a new array, not modify nums.
Solution
Step 1: Check index calculations for swapping
The code uses nums.length - i as the index for the element to swap, but array indices go from 0 to nums.length - 1, so it should be nums.length - i - 1.
Step 2: Confirm loop condition and swapping logic
The loop condition i < nums.length / 2 is acceptable, and temp is needed for swapping. Modifying the original array is allowed here.
Final Answer:
The index nums.length - i is out of bounds; should be nums.length - i - 1. -> Option A
Quick Check:
Array indices start at 0, so subtract 1 [OK]
Hint: Array last index = length - 1, not length [OK]
Common Mistakes:
Using nums.length - i instead of nums.length - i - 1
Removing temp variable causing swap errors
Incorrect loop boundary causing extra swaps
5. Given an array arr, which code snippet correctly reverses the array without modifying the original array?
hard
A. const reversed = arr.reverse();
B. const reversed = arr.slice().reverse();
C. const reversed = [...arr].sort().reverse();
D. const reversed = arr.reverse().map(x => x);
Solution
Step 1: Understand how to avoid modifying original array
Using arr.slice() creates a shallow copy of the array. Calling reverse() on this copy reverses it without changing the original.
Step 2: Analyze other options
const reversed = arr.reverse(); reverses the original array. const reversed = [...arr].sort().reverse(); sorts before reversing, changing order. const reversed = arr.reverse().map(x => x); reverses the original array because reverse() is called on arr before mapping.
Final Answer:
const reversed = arr.slice().reverse(); -> Option B
Quick Check:
Copy then reverse to keep original safe [OK]
Hint: Use slice() to copy before reverse() [OK]
Common Mistakes:
Using reverse() directly modifies original array
Sorting before reversing changes order
Thinking reverse().map(x => x) preserves original array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.