If you followed my Algorithm 101 first episode, then this will be very easy. Here, the main objective is to reverse a given word and check if it still matches the given word.
wordPalindrome("Racecar"); // true
wordPalindrome("Race car"); // false
In how many ways can you achieve this? I have 6 ways to Check if a given Word is a Palindrome. I know you will like to check them out.
This article will focus on word palindrome
only. In a future episode, we will be looking at sentence palindrome.
Prerequisite
This article assumes that you have basic understanding of javascript's string and array methods.
Let's Check if a Word is a Palindrome using:
- toLowerCase(), split(), reverse(), join(), if...statement;
function wordPalindrome(word) {
let lowerCasedWord = word.toLowerCase();
let newWord = lowerCasedWord
.split("")
.reverse()
.join("");
if (newWord === lowerCasedWord) {
return true;
}
return false;
}
- toLowerCase(), spread operator, reverse(), join(), if...statement;
function wordPalindrome(word) {
let lowerCasedWord = word.toLowerCase();
let newWord = [...lowerCasedWord].reverse().join("");
if (newWord === lowerCasedWord) {
return true;
}
return false;
}
- toLowerCase(), spread operator, if...statement
function wordPalindrome(word) {
let lowerCasedWord = word.toLowerCase();
let newWord = [...lowerCasedWord].reduce((total, acc) => acc + total);
if (newWord === lowerCasedWord) {
return true;
}
return false;
}
- toLowerCase(), for...loop, join(), if...statement;
function wordPalindrome(word) {
let lowerCasedWord = word.toLowerCase();
let newWord = [];
for (let i = lowerCasedWord.length; i >= 0; i--) {
newWord.push(lowerCasedWord[i]);
}
if (newWord.join("") === lowerCasedWord) {
return true;
}
return false;
}
- toLowerCase(), for...of...loop, if...statement;
function wordPalindrome(word) {
let lowerCasedWord = word.toLowerCase();
let newWord = "";
for (char of lowerCasedWord) {
newWord = char + newWord;
}
if (newWord === lowerCasedWord) {
return true;
}
return false;
}
- toLowerCase(), for...in...loop, if...statement;
function wordPalindrome(word) {
let lowerCasedWord = word.toLowerCase();
let newWord = "";
for (char in lowerCasedWord) {
newWord = lowerCasedWord[char] + newWord;
}
if (newWord === lowerCasedWord) {
return true;
}
return false;
}
Conclusion
There are many ways to solve problems programmatically. You are only limited by your imagination. I will love to know other ways you solved yours in the comment section.
If you have questions, comments or suggestions, please drop them in the comment section.
You can also follow and message me on social media platforms.
Thank You For Your Time.
Top comments (8)
Nice one. I think just doing
Should work for the if statement and return false line
Thank you Blessing. I will definitely check that out
In fact, I just did and I love the simplicity
dev-to-uploads.s3.amazonaws.com/i/...
Good post. Really liked various ways you have noted. I will suggest one more. It should work too. Let me know...
Use two pointers starting from 0th and (string.length-1)th index, increase one and decrease the other respectively, till they meet at the center and compare characters at those pointers. If they match continue, else return false.
The advantage you gain is no excess space is required.
That is what I would do.
That approach is also useful to reverse arrays, just swap
0th <-> (len-1)th
and so on.I will definitely check it out. Sounds like divide-and-conquer. Lol
Good day sir. Please I have a challenge on how to derive an algorithm and draw a flowchart to determine if a word is a palindrome. And also I would like to use c++ program to express the algorithm
It's been a while I wrote C++. I don't even know if I can remember the syntax. Lol.
However, you can easily convert any of my solutions to C++ if you understand both JS and C++