📖 Scenario: Imagine you are building a simple tool that helps users reverse words or sentences. This is useful in games, puzzles, or text processing apps.
🎯 Goal: You will create a program that takes a string and reverses it using different methods step-by-step.
📋 What You'll Learn
Create a string variable with a specific value
Create a helper variable to hold the reversed string
Use a for loop to reverse the string manually
Print the reversed string
💡 Why This Matters
🌍 Real World
Reversing strings is useful in text processing, puzzles, and coding challenges.
💼 Career
Understanding string manipulation is a basic skill for software developers working with user input and data transformation.
Progress0 / 4 steps
1
Create the original string
Create a string variable called original and set it to the value "hello".
DSA Typescript
Hint
Use const to create a string variable named original and assign it the text "hello".
2
Create a variable to hold the reversed string
Create a string variable called reversed and set it to an empty string "".
DSA Typescript
Hint
Use let to create a variable named reversed and start it as an empty string.
3
Reverse the string using a for loop
Use a for loop with a variable i starting from original.length - 1 down to 0. Inside the loop, add original[i] to reversed.
DSA Typescript
Hint
Start the loop from the last character index and move backward to the first. Add each character to reversed.
4
Print the reversed string
Use console.log to print the value of reversed.
DSA Typescript
Hint
Use console.log(reversed) to show the reversed string on the screen.
Practice
(1/5)
1. What does the split('') method do when applied to a string in TypeScript?
easy
A. It reverses the string directly.
B. It converts the string into an array of individual characters.
C. It joins the string elements into one string.
D. It removes all spaces from the string.
Solution
Step 1: Understand the split method
The split('') method breaks a string into an array of characters by splitting at every empty string between characters.
Step 2: Effect on string
For example, "hello".split('') becomes ["h", "e", "l", "l", "o"].
Final Answer:
It converts the string into an array of individual characters. -> Option B
Quick Check:
split('') = array of chars [OK]
Hint: split('') breaks string into chars array [OK]
Common Mistakes:
Thinking split('') reverses the string
Confusing split('') with join()
Assuming split('') removes spaces
2. Which of the following is the correct syntax to reverse a string str using built-in methods in TypeScript?
easy
A. str.split('').reverse().join('')
B. str.reverse().join('')
C. reverse(str).split('')
D. str.join('').reverse()
Solution
Step 1: Split string into array
Use str.split('') to convert string into array of characters.
Step 2: Reverse and join
Use reverse() on array, then join('') to form reversed string.
Final Answer:
str.split('').reverse().join('') -> Option A
Quick Check:
split + reverse + join = reversed string [OK]
Hint: Split, reverse array, then join to reverse string [OK]
Common Mistakes:
Calling reverse() directly on string
Using join() before reverse()
Using reverse() as a standalone function
3. What will be the output of the following TypeScript code?