hint [3/9]
You may use regular expressions (RegExp
) to remove unwanted characters from the string.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/9]
You may use Array.prototype.split
and Array.prototype.join
methods or for
loop.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [5/9]
You may lowercase the string using String.prototype.toLowerCase
.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/9]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [7/9]
function palindrome(str) {
var normalizedStr = str.replace(/[\W_]/g, '').toLowerCase();
var reverseStr = normalizedStr.split('').reverse().join('');
return normalizedStr === reverseStr;
}
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
/[\W_]/g
hint [1/9]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/9]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [2/9]
You have to check if the string you get as an input is a palindrome and return true
if it is, and false
otherwise.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [3/9]
You may use regular expressions (RegExp
) to remove unwanted characters from the string.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/9]
You may use Array.prototype.split
and Array.prototype.join
methods or for
loop.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [1/12]
Make sure you've tried to hard to solve it yourself before proceeding. :construction:
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [2/12]
Our goal for solving this problem is tidying up the string passed in, and checking whether it is in fact a palindrome.
mom
, when you reverse the letters, it spells the same thing! Another example of a palindrome is race car
. When we take out anything that is not a character it becomes racecar
which is the same spelled forwards or backwards!Once we have determined whether it is a palindrome or not we want to return either true
or false
based on our findings.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [3/12]
Regular expressions, RegEx
, can be used to remove unwanted characters from the string.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [4/12]
The Array.prototype.split
and Array.prototype.join
methods can be of use here. For
and while
loops are another alternative, or why not even map
!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [5/12]
String.prototype.toLowerCase
can be used to make a string lowercase.
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [6/12]
Solution ahead!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [7/12]
function palindrome(str) {
return str.replace(/[\W_]/g, '').toLowerCase() ===
str.replace(/[\W_]/g, '').toLowerCase().split('').reverse().join('');
}
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [8/12]
function palindrome(str) {
str = str.toLowerCase().replace(/[\W_]/g, '');
for(var i = 0, len = str.length - 1; i < len/2; i++) {
if(str[i] !== str[len-i]) {
return false;
}
}
return true;
}
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [9/12]
(using recursion)
function palindrome(str) {
// make all letters lowercase and remove non-alphanumeric characters
str = str.toLowerCase().replace(/[^a-z|1-9]/g, "");
// if the length of the string is 0 then it is a palindrome
if (str.length === 0){
return true;
}
// if the first letter and the last letter of the string
// do not equal each other, then it is not a palindrome
if (str[0] !== str[str.length-1]){
return false;
}
//Else, run the function without the first and last characters.
else{
return palindrome(str.slice(1,str.length - 1));
}
}
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]
hint [10/12]
We start by using regular expressions to replace any white space or non-alphanumeric characters with nothing (or null
), which essentially removes them from the string.
Next we chain .toLowerCase()
to remove any capital letters because A
is a different character than a
. The problem did not ask us to worry about making sure the case of the characters was identical, just the spelling.
Our next step is to take our string and .split()
it, .reverse()
it, and finally .join()
it back together.
Last step is to check that the string is the same forwards and backwards and return our result!
type
hint
for next hint :pencil: [Contribute at the FCC Wiki]