Kept mine simple, all uppercase, removed extra characters, then flipped it around to check for palindrome
function palindrome(str) {
var palin = str.toUpperCase().replace(/[\W_]/gi,"");
var result = palin.split('').reverse().join('');
return palin === result;
}
palindrome("eye");
console.log(palindrome("level"))
Hi all! Here’s my code for the palindrome project. I put the guidelines of the rules in comments of my code.
// THE STEPS FOR PROJECT ARE:
// 1. Receive words (from input/user)
// 1a. Palindromes are words that can be spelled backwards and forwards; it doesn’t count spacing or punctuation
// 2. Remove ALL Non-Alphanumberic Characters (punctuation, spaces, symbols)
// 2a. Turn everything into the same case
// 3. Check words and confirm that they are/aren’t palindromes
// 4. Code needs to PASS varying formats of the words. ie: racecar, RaceCar, and race CAR.
// 5. Codes needs to PASS strings w/ special symbols. ie: 2A33a2, 2A3 3a2, and 2_A33#A2y
function removeNonAlpha(str) {
// step 2
return str.replace(/[\W_]/g, “”);
}
function sameCaseStr(str) {
// step 2a
return str.toString().toLowerCase();
}
function reverseString(str) {
// step 3
return str.toString().split(“”).reverse().join(“”);
}
function palindrome(str) {
// removes non-alphanumeric characters
var nonAlphaString = removeNonAlpha(str);
// makes case in string all lowercase
var lowerCaseString = sameCaseStr(nonAlphaString);
// check/confirm if words are palindromes
var reservedString = reverseString(lowerCaseString);
// compare string; if the same, return true
console.log(`Is ${lowerCaseString} the palindrome of ${reservedString}?`)
return lowerCaseString === reservedString;
I decided to solve it without using methods like .join since at this point we did not learn stuff like that. I will improve the code later.
function palindrome(str) {
let result = "";
//looping through the string
for (let i = str.length - 1; i >= 0; i--) {
result = result + str[i];
}
//cleaning the string and result to discard special characters
let newResult = result.replace(/[^a-z0-9]/gi, "");
let newStr = str.replace(/[^a-z0-9]/gi, "");
if (newStr.toLowerCase() !== newResult.toLowerCase()) {
return false;
}
return true;
}
palindrome("not a palindrome");