Coding for Scrubs: Palindromes (JS)

Welcome to my series called ‘Coding for Scrubs.’ I am new to the coding world so to help myself become a better programmer, I am going to break down different problems. These problems will be coming from all sorts of sources such as CodeWars, LeetCode, possible whiteboarding problems, etc. The series are mainly for my learning purposes (because hi, hello, I am the scrub. That’s what the S stands for in my name). If you’ve stumbled upon these articles, I hope you benefit from them! If you also catch any errors, please please PLEASE let me know. I appreciate any sort of criticism!

1. Define what a palindrome is:

A palindrome is a word, sentence, numbers, or a series of characters that are the same whether written backward or forward. For example racecar, taco cat, a man, a plan, a canal-panamaand 8112118. If the length of the word is even, we would know that the first half is mirrored. If the length of the word is odd, we can ignore the middle letter and compare the rest of the letters because we know that would also be a palindrome.

To keep it simple for now, we will create a function that will check if a word is a palindrome.

2. In plain English, write what steps we have to take:

Now we know what a palindrome is and we know we are passing in a string. Because a palindrome is the same written forward and backward, we know that we will first have to iterate through the letters in the word.

1. Split the length of the word in half
2. Create a loop to iterate through the first half of the word
3. Loop through each letter in the string starting at the end and moving backwards
4. If #1 does not equal to #2, then return false, return true

We can see that a palindrome is a word or phrase that if split in half, should mirror each other. Unless the word consists of an odd number of characters, it should then ignore the middle letter.

3. Create a function to test!

Personally, I like to check and make sure everything in my VSCode is running. Let’s just check real quick that we can console.log the string that we’re passing in!

function isPalindrome(string) {
console.log(string)
}
console.log(isPalinedrome("hello"));#=> hello

Awesome.

If we look back at our pseudocode, we first have to split the length of the string in half.

function isPalindrome(string){
var halfString= Math.floor(string.length / 2);
}

Next, we know we have to iterate, let’s go ahead and make a for loop!

function isPalindrome(string){
var halfString= Math.floor(string.length / 2);
for(let i = 0; i < halfString.length; i++){
console.log(string[i])
}
}

We are also going to console.log to make sure we can get each letter out from our string. Next, we have to console.log the string in reverse.

function isPalindrome(string){
var halfString = Math.floor(string.length / 2);
for(let i = 0; i < halfString.length; i++){
console.log(string[i])
console.log(string[string.length - i - 1]
}
}

This is how we will get the string in reverse.

Now we just compare the two and test!

function isPalindrome(string){
var halfString= Math.floor(string.length / 2);
for (var i = 0; i < halfString.length; i++){
if (string[i] !== string[string.length - i - 1]){
return false;
}
return true;
}
}
console.log(isPalindrome("tacocat")); #=> true
console.log(isPalindrome("tacodog")); #=> false

WOOHOO!

If we are passing a string that has symbols, we can use regex to remove them. If the string also contains upper case letters, we can convert all of them to lowercase.

funciton isPalindrome(string{
string = string.replace(/\W/g, ''); // removes symbols
string = string.toLowerCase(); // converts to lowercase
var halfString = Math.floor(string.length / 2);
for (var i = 0; i < halfString,length; i++){
if (string[i] !== string[string.length - i - 1]){
return false;
}
return true;
}
}

Software Engineer in the making.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store