Reputation: 49
I'm attempting to create a function to check if a string is a palindrome. Most of the tests work but the following do not work with my code: (_eye, almostomla, My age is 0, 0 si ega ym.) Here is my function:
function palindrome(str) {
var specChar = "/\D/g";
var array = str.trim().replace(specChar, '').toLowerCase().split('');
var array2 = str.trim().replace(specChar, '').toLowerCase().split('').reverse();
for (var i = 0; i < array.length; i++) {
if (array[i] === array2[i]) {
return true;
} else {
return false;
}
}
}
Upvotes: 1
Views: 66
Reputation: 15453
I think a more elegant solution can arise out of using the every()
function also available on MDN Web Docs
You are on the right track with splitting the string into an array because as you know we cannot call every()
or map()
or any array helper method on a string, only on arrays, but you may want to consider it like this:
function palindrome(str) {
str.split('')
}
Then, as I was suggesting you can attach every()
to it like so:
function palindrome(str) {
str.split('').every()
}
Why every()
?
The function will be called for every element in the array. It will take a first argument of a fat arrow function like so:
function palindrome(str) {
str.split('').every(() => {
});
}
And the arrow function will take a first argument of char
which represents every element in the array:
function palindrome(str) {
str.split('').every((char, ) => {
});
}
And because I want to compare each element to its mirror on the other side, how do we gain access to the other side elements?
So fortunately as a second argument to this function we are given the index of the element which I will record as i
like so:
function palindrome(str) {
str.split(‘’).every((char, i) => {
});
}
The first time this inner function is called i
will be equal to zero.
Inside this every function I can return my comparison between the first element and the mirrored element on the other side of the array.
Getting access to the other side is a bit complex.
So the first time I run this function its at index of zero.
So i = 0
, to get access to the element on the other side we look at the entire string array and access the element at the length
of the array - 1
.
All the arrays in JavaScript are zero indexed.
Why minus 1? Because there are more than zero elements in the array, so you can’t say look at the elements .length
, you have to say look at the elements .length - 1
like so:
function palindrome(str) {
str.split(‘’).every((char, i) => {
return char === str[str.length - i - 1];
});
}
Minus 1 is to ensure that we take into account that the length is the overall length but we are zero indexed with JavaScript arrays.
Lastly, we need to ensure we return the result of the every() call like so:
function palindrome(str) {
return str.split(‘’).every((char, i) => {
return char === str[str.length - i - 1];
});
}
Upvotes: 0
Reputation: 596
MDN has a really neat way of checking for palindromes: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Quote:
var str = '12345';
Array.prototype.map.call(str, function(x) {
return x;
}).reverse().join('');
// Output: '54321'
// Bonus: use '===' to test if original string was a palindrome
Also, don't forget to take into consideration data type conversions, numbers, and empty strings.
Hope this helps.
Upvotes: 1