📖 Scenario: Imagine you are building a simple tool that helps users reverse words they type. This is useful in games, puzzles, or just for fun.
🎯 Goal: You will create a program that takes a fixed word, then reverses it using a step-by-step approach. You will learn how to work with strings and loops in JavaScript.
📋 What You'll Learn
Create a string variable with a specific word
Create a variable to hold the reversed string
Use a for loop to reverse the string character by character
Print the reversed string
💡 Why This Matters
🌍 Real World
Reversing strings is useful in puzzles, coding challenges, and some text processing tasks.
💼 Career
Understanding string manipulation and loops is fundamental for many programming jobs, including web development and software engineering.
Progress0 / 4 steps
1
Create the original string
Create a string variable called original and set it to the exact value "hello".
DSA Javascript
Hint
Use const to create a string variable named original and assign it the word "hello".
2
Create a variable to hold the reversed string
Create a string variable called reversed and set it to an empty string "".
DSA Javascript
Hint
Use let to create a variable named reversed and assign it 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 Javascript
Hint
Start the loop from the last character index and move backwards to the first character, adding each character to reversed.
4
Print the reversed string
Write a console.log statement to print the value of reversed.
DSA Javascript
Hint
Use console.log(reversed) to show the reversed word in the console.
Practice
(1/5)
1. What does the JavaScript method split('') do when applied to a string?
easy
A. It reverses the string characters.
B. It removes all spaces from the string.
C. It joins array elements into a string.
D. It converts the string into an array of individual characters.
Solution
Step 1: Understand the split method
The split('') method splits a string into an array by each character because the separator is an empty string.
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 D
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 in JavaScript using built-in methods?
easy
A. str.reverse().join('')
B. str.split('').reverse().join('')
C. reverse(str).split('').join('')
D. str.join('').reverse()
Solution
Step 1: Understand string and array methods
Strings do not have a reverse() method, but arrays do. So first convert string to array using split('').
Step 2: Reverse array and join back
Use reverse() on the array, then join('') to convert back to string.