📖 Scenario: You are building a simple program to check if a word is a palindrome. A palindrome is a word that reads the same forwards and backwards, like "level" or "radar".This is useful in games, puzzles, and text analysis.
🎯 Goal: Build a TypeScript program that checks if a given word is a palindrome and prints true if it is, or false if it is not.
📋 What You'll Learn
Create a string variable with a specific word
Create a helper variable to hold the reversed word
Write logic to reverse the string
Compare the original and reversed strings
Print the result as true or false
💡 Why This Matters
🌍 Real World
Palindrome detection is used in text processing, coding challenges, and games to find words or phrases that read the same forwards and backwards.
💼 Career
Understanding string manipulation and loops is fundamental for software development, especially in roles involving data processing, algorithms, and coding interviews.
Progress0 / 4 steps
1
Create the word variable
Create a string variable called word and set it to the exact value "racecar".
DSA Typescript
Hint
Use const to create a variable named word and assign the string "racecar".
2
Create a reversed word variable
Create a string variable called reversedWord and set it to an empty string "".
DSA Typescript
Hint
Use let to create a variable named reversedWord and assign it an empty string.
3
Reverse the word using a loop
Use a for loop with a variable i starting from word.length - 1 down to 0. Inside the loop, add word[i] to reversedWord.
DSA Typescript
Hint
Loop backwards over the string and add each character to reversedWord.
4
Compare and print the result
Print true if word is equal to reversedWord, otherwise print false. Use console.log(word === reversedWord).
DSA Typescript
Hint
Use console.log to print the comparison result of word === reversedWord.
Practice
(1/5)
1. What does it mean if a string is a palindrome?
easy
A. The string has no spaces
B. The string reads the same forwards and backwards
C. The string contains only letters
D. The string is all uppercase
Solution
Step 1: Understand palindrome definition
A palindrome is a word or phrase that reads the same forwards and backwards.
Step 2: Compare options with definition
Only The string reads the same forwards and backwards matches this definition exactly.
Final Answer:
The string reads the same forwards and backwards -> Option B
Quick Check:
Palindrome = same forwards and backwards [OK]
Hint: Palindrome means same forwards and backwards [OK]
Common Mistakes:
Confusing palindrome with uppercase or letter-only strings
Thinking palindrome means no spaces
Assuming palindrome means all lowercase
2. Which of the following TypeScript code snippets correctly checks if a string str is a palindrome?
easy
A. return str === str.split().reverse().join();
B. return str === str.reverse();
C. return str === str.split('').reverse().join('');
D. return str === str.split('').reverse();
Solution
Step 1: Understand string reversal in TypeScript
To reverse a string, split it into characters, reverse the array, then join back.
Step 2: Check each option's correctness
return str === str.split('').reverse().join(''); uses split(''), reverse(), join('') correctly. Others miss correct methods or arguments.
Final Answer:
return str === str.split('').reverse().join(''); -> Option C
reversed is an array after split and reverse, not a string.
Step 2: Compare string with array
Comparing string to array returns false always; need to join array back to string.
Final Answer:
reversed is an array, not a string -> Option D
Quick Check:
Compare string to string, not array [OK]
Hint: Join reversed array back to string before comparing [OK]
Common Mistakes:
Comparing string directly to array
Forgetting to join reversed array
Assuming split('') returns string
5. You want to check if a sentence is a palindrome ignoring spaces, punctuation, and case. Which approach is best?
hard
A. Convert to lowercase, remove non-alphanumeric chars, then compare with reversed
B. Compare original string with reversed string directly
C. Remove spaces only, then compare
D. Convert to uppercase and compare with reversed uppercase
Solution
Step 1: Understand palindrome ignoring spaces and punctuation
We must clean the string by removing spaces and punctuation and normalize case.
Step 2: Choose correct cleaning and comparison
Convert to lowercase, remove non-alphanumeric chars, then compare with reversed cleans string by removing non-alphanumeric characters and lowercases it before comparing with reversed.
Final Answer:
Convert to lowercase, remove non-alphanumeric chars, then compare with reversed -> Option A
Quick Check:
Clean and normalize before palindrome check [OK]
Hint: Clean string fully before palindrome check for accuracy [OK]
Common Mistakes:
Only removing spaces but not punctuation
Comparing without case normalization
Assuming uppercase conversion alone is enough
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.