So I picked another small problem, many interviewers asked this question to observe candidates’ understanding of Javascript, that is Find a palindrome.
Even though I’ve asked this question many times during the interview, keep in mind that I am trying to propose as simple a solution as possible.
if you copy and paste the below-mentioned code your browser’s console, you will be able to see how does this code works.
Solution 1:
function isPalindrome(str) {
return str == str.split('').reverse().join('');
}
Solution 2:
function palindrome(str) {
var re = /[\W_]/g;
var lowRegStr = str.toLowerCase().replace(re, '');
var reverseStr = lowRegStr.split('').reverse().join('');
return reverseStr === lowRegStr;
}