📖 Scenario: You are working on a simple app that processes a list of daily temperatures recorded in Celsius. Sometimes, you need to reverse the order of these temperatures to analyze trends backwards.
🎯 Goal: Build a TypeScript program that creates an array of temperatures, sets up a helper variable for the array length, reverses the array using a loop, and finally prints the reversed array.
📋 What You'll Learn
Create an array called temperatures with these exact values: 15, 22, 18, 30, 25
Create a variable called n that stores the length of the temperatures array
Use a for loop with a variable i to reverse the temperatures array in place
Print the reversed temperatures array using console.log
💡 Why This Matters
🌍 Real World
Reversing arrays is useful in data analysis, such as looking at time series data backwards or undoing operations.
💼 Career
Understanding array manipulation is fundamental for software developers, data analysts, and anyone working with collections of data.
Progress0 / 4 steps
1
Create the temperatures array
Create an array called temperatures with these exact values: 15, 22, 18, 30, 25
DSA Typescript
Hint
Use const temperatures: number[] = [15, 22, 18, 30, 25]; to create the array.
2
Create a variable for the array length
Create a variable called n that stores the length of the temperatures array
DSA Typescript
Hint
Use const n: number = temperatures.length; to get the array length.
3
Reverse the array using a for loop
Use a for loop with a variable i to reverse the temperatures array in place by swapping elements
DSA Typescript
Hint
Swap elements from start and end moving towards the center using a loop.
4
Print the reversed array
Print the reversed temperatures array using console.log(temperatures)
DSA Typescript
Hint
Use console.log(temperatures); to print the reversed array.
Practice
(1/5)
1. What does the reverse() method do to an array in TypeScript?
easy
A. It reverses the order of elements in the original array.
B. It sorts the array in ascending order.
C. It removes duplicate elements from the array.
D. It creates a new array with elements doubled.
Solution
Step 1: Understand the reverse() method
The reverse() method changes the order of elements in the array so the last element becomes first and the first becomes last.
Step 2: Confirm the effect on the original array
This method modifies the original array itself, not creating a new one.
Final Answer:
It reverses the order of elements in the original array. -> Option A
Quick Check:
reverse() reverses array order [OK]
Hint: Remember: reverse() flips the array 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 TypeScript?
easy
A. arr->reverse();
B. reverse(arr);
C. arr = reverse();
D. arr.reverse();
Solution
Step 1: Identify the method call on the array
In TypeScript, reverse() is a method called directly on the array object using dot notation.
Step 2: Verify syntax
arr.reverse(); is correct. reverse(arr); incorrectly treats reverse as a standalone function. arr = reverse(); misuses the method. arr->reverse(); uses invalid syntax.
Final Answer:
arr.reverse(); -> Option D
Quick Check:
Use dot notation: arr.reverse() [OK]
Hint: Use dot notation: array.reverse() [OK]
Common Mistakes:
Calling reverse() as a standalone function
Assigning reverse() without calling on array
Using incorrect symbols like '->'
3. What is the output of the following TypeScript code?
C. reverse method should be called with an argument.
D. Array cannot be reversed using reverse method.
Solution
Step 1: Check how reverse is used
The code assigns numbers.reverse without parentheses, so it assigns the function itself, not the reversed array.
Step 2: Correct usage of reverse()
To reverse the array, the method must be called with parentheses: numbers.reverse().
Final Answer:
Missing parentheses to call reverse method. -> Option B
Quick Check:
Call reverse() with () to execute [OK]
Hint: Always use () to call methods like reverse() [OK]
Common Mistakes:
Forgetting parentheses when calling methods
Thinking reverse needs arguments
Believing reverse is not a valid method
5. Given the array const data = [10, 20, 30, 40, 50];, which code snippet correctly reverses the array without modifying the original array?
hard
A. const reversed = data.reverse();
B. const reversed = data.sort().reverse();
C. const reversed = [...data].reverse();
D. const reversed = data.map().reverse();
Solution
Step 1: Understand the problem requirement
We want to reverse the array but keep the original array unchanged.
Step 2: Analyze each option
const reversed = data.reverse(); reverses the original array directly, modifying it. const reversed = [...data].reverse(); creates a shallow copy using spread [...data] and then reverses the copy, preserving the original. const reversed = data.sort().reverse(); sorts the original array first, changing order before reversing, and modifies original. const reversed = data.map().reverse(); uses map() incorrectly without a callback, causing error.
Final Answer:
const reversed = [...data].reverse(); -> Option C
Quick Check:
Copy then reverse to keep original safe [OK]
Hint: Copy array first with [...array] before reverse() [OK]
Common Mistakes:
Using reverse() directly modifies original array
Sorting before reversing changes order incorrectly
Using map() without a callback causes error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.