A palindrome Javascript exam question

Hi fellas. i’m working on the front end web dvelopment course and got stuck on this question.

A palindrome is a word or sentence that is spelled the same way both forward and backward. Below we’ve provided you with a function that will check if the string provided is a palindrome, but our code is broken.

Using the error messages in the console and your knowledge of debugging, locate and resolve the errors in our code so that it executes properly.

function checkPalindrome(str) {
const reversedStr = ‘’;

for(let i = reversedStr.length-1; i >= 0; i–) {
return reversedStr += str[i];
}

if (reversedStr.split(" “).join(‘’) === reversedStr.split(” ").join(‘’)) {
return The word or sentence, "${str}", is a palindrome!;
} else {
return The word or sentence, "${str}", is not a palindrome.;
}
};

console.log(checkPalindrome(‘may a moody baby doom a yam’));

When I run this code it logs
The word or sentence, “may a moody baby doom a yam”, is a palindrome!

Seems working fine but uf i click on “check answer” , “Try checking your code again. You likely have a syntax error” pops up.

Since its one of those Exam questions I don’t want to get a direct answer but can anyone guide me to a right direction?

1 Like

What has that to do with inputs?

Are we expected to repair broken code? If so I would just revert to the built in array function.

 > isPal = function(x) {
       return x === x.split('').reverse().join('')
   }
<- ƒ (x) {
       return x === x.split('').reverse().join('')
   }
 > isPal('121')
<- true
 > 

However, this is only a string version. What about palidrome numbers, or even data sets (as if we’re going that far)?

What about palindromes where spaces get rearranged and that may contain non-word characters with mixed letter case?

“A man, a plan, a canal—Panama,”

Aside

This is where it gets freaky:

 > isPal = function(x) {
       return x.toLowerCase().split(/\W/).join('').split('').join('') === x.toLowerCase().split(/\W/).join('').split('').reverse().join('')
   }
<- ƒ (x) {
       return x.toLowerCase().split(/\W/).join('').split('').join('') === 
   x.toLowerCase().split(/\W/).join('').split('').reverse().join('')
   }
 > isPal('A man, a plan, a canal—Panama,')
<- true
 > 

Best buckle up when Regular Expressions come into play. They are costly in more ways than one. However, in the rare instance they can be rather handy. It will take a year or more to study them so just take it one bite at a time as you progress.

In your own experiments, leave off the extra split and join, and study the results (the filtered/split strings). You will then discover why it is necessary to do the added action.

 > 'A man, a plan, a canal—Panama,'.toLowerCase().split(/\W/).reverse().join('')
<- 'panamacanalaplanamana'
 > 

Earlier we talked about palindromic numbers. We can add that to the fray just by converting all the input to string before invoking .toLowerCase().

isPal = function(x) {
    return x.toString(10).toLowerCase().split(/\W/).join('').split('').join('') === x.toString(10).toLowerCase().split(/\W/).join('').split('').reverse().join('')
}

 > isPal(11**4)
<- true
 >
2 Likes

were you able to actually figure this out? I’m having a tough time as well.

1 Like

I’m right there with you, after about 40 minutes I said forget it. Best of luck on your journey!

Thanks. you too. I’ll be attening a bootcamp soon as well.

I GOT ITTTTT!!!

After what seemed like an eternity

function checkPalindrome(str) {
let reversedStr = ‘’;

for(let i = str.length-1; i >= 0; i–) {
reversedStr += str[i];
}

if (str.split(" “).join(”“) === reversedStr.split(” “).join(”")) {
return The word or sentence, "${str}", is a palindrome!;
} else {
return The word or sentence, "${str}", is not a palindrome.;
}
};

console.log(checkPalindrome(‘may a moody baby doom a yam’));

1 Like