JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Tell us what’s happening:
So I’ve tried several ways for the palidrome checker. I don’t like copying other peoples code because it defeats the object of learning to write the code yourself. This is the newest version of my attempts (which have been many :expressionless: ) but I feel like I’m still missing something as it doesn’t work at all

   **Your code so far**
//* .match(/\w+\S+/); ???

function palindrome(str) {
 let newStr = str.toLowerCase("");
 let newStrArr = newStr.split("");
 
//*CYCLES THROUGH AND RETURNS ALPHANUMERIC VALUES ONLY
 for (let i=0; i <= newStrArr.length; i++) {
   let removed = newStrArr.slice(/\W+/);
   let removedJoin = removed.join("");
   return removedJoin;
 } 

//*REVERSES (LET REMOVED) 
 let removedReverse = ("");

 for (let i=0; i <= removed.length - 1; i--) { 
   let removedReverseJoin = removedReverse.join("");
   return removedReverseJoin;    
 }

//* Declares boolean
 if (removedJoin == removedReverseJoin) {
   return true;
 }

 else {
 return false;
}  
} 
  








palindrome("eye");
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.71

Challenge: JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Link to the challenge:

Hey.
I just called your function:

console.log(palindrome("eye"));//eye

It returns string instead of boolean value. I think you can start debugging from dealing with it. I guess you’ll find the reason why it happens. If not, let me know, I will give some directions.

UPDATE. I used some recursion to solve this, but you can do it with iterations also. And maybe you want to use reverse() somewhere in the code.

Let start here. You sure this is doing what you think it should be? One hint ill give you is return will return whatever you give it to where the function was called and stop a loop. There are other problems, but im going to try and let you de-bug this as much as you can since this is an end project. Toss as many console.logs as you need to see how things are working.

1 Like

Thank you. I thought the first return statement might of been unnecessary. I was avoiding the .reverse() function as it kept displaying an error and I couldn’t figure out why. I think the reverse function works now but I still have an issue with the code

Share your code then, lets figure it out

Thanks I’ve removed the unnecessary return statement as I just want it to return true or false. I’ll get there eventually. It’s tweaked slightly but still not fully working.

function palindrome(str) {
  let newStr = str.toLowerCase("");
  let newStrArr = newStr.split("");
  
//* CYCLES THROUGH ARRAY 
  for (let i=0; i < newStrArr.length; i++) {
//* RETURNS ALPHANUMERIC VALUES ONLY
    let toRemove = (/\w+/);
    let removed = newStrArr.slice(toRemove);

//* CONCATENATES BACK TO STRING
    let removedJoin = removed.join("");

//* REVERSES (let removed) AND STORES IN NEW VARIABLE
    let removedReverse = removed.reverse("");

//* CONCATENATES REVERSED ARRAY TO STRING
    let removedReverseJoin = removedReverse.join("");

//*CONDITIONAL STATEMENT/RETURNS BOOLEAN    
    
    if (removedJoin == removedReverseJoin) {
    return true;
  }
  return false;  
 }
}      

palindrome("eye");

Hi. Okay so I thought .slice() would return a new array with just the alphanumeric values. using console.log(remove) displays [ ‘e’, ‘y’, ‘e’ ]

And the for loop I thought was needed to cyle through the entire array. I’m thinking now that can be done with a regex and the +

I added some logging and test cases to your code. Hope it helps.
There is also output after running the below.

function palindrome(str) {
  console.log('testCASE  ', str);//CHANGED
  let newStr = str.toLowerCase("");
  let newStrArr = newStr.split("");
  
//* CYCLES THROUGH ARRAY 
  for (let i=0; i < newStrArr.length; i++) {
//* RETURNS ALPHANUMERIC VALUES ONLY
    let toRemove = (/\w+/);
    console.log('toRemove value after regexp: ', toRemove)//CHANGED
    let removed = newStrArr.slice(toRemove);
    console.log('removed value after slicing: ', removed)//CHANGED

//* CONCATENATES BACK TO STRING
    let removedJoin = removed.join("");

//* REVERSES (let removed) AND STORES IN NEW VARIABLE
    let removedReverse = removed.reverse("");

//* CONCATENATES REVERSED ARRAY TO STRING
    let removedReverseJoin = removedReverse.join("");

//*CONDITIONAL STATEMENT/RETURNS BOOLEAN    
    
    if (removedJoin == removedReverseJoin) {
    return true;
  }
  return false;  
 }
}

/*array of FCC test cases*/
const testCases = [
  ["eye", true],
  ["_eye", true],
  ["race car", true],
  ["not a palindrome", false],
  ["A man, a plan, a canal. Panama", true],
  ["never odd or even", true],
  ["nope", false],
  ["almostomla", false],
  ["My age is 0, 0 si ega ym.", true],
  ["1 eye for of 1 eye.", false],
  ["0_0 (: /-\\ :) 0-0", true],
  ["five|\\_/|four", false]
  ];

for (test of testCases) {
  console.log('for string ===', test[0], ' expected: ', test[1],
  'return: ', palindrome(test[0]));
  console.log('-----------');
}

/*
Output:

testCASE   eye
toRemove value after regexp:  /\w+/
removed value after slicing:  [ 'e', 'y', 'e' ]
for string === eye  expected:  true return:  true
-----------
testCASE   _eye
toRemove value after regexp:  /\w+/
removed value after slicing:  [ '_', 'e', 'y', 'e' ]
for string === _eye  expected:  true return:  false
-----------
testCASE   race car
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  'r', 'a', 'c',
  'e', ' ', 'c',
  'a', 'r'
]
for string === race car  expected:  true return:  false
-----------
testCASE   not a palindrome
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  'n', 'o', 't', ' ',
  'a', ' ', 'p', 'a',
  'l', 'i', 'n', 'd',
  'r', 'o', 'm', 'e'
]
for string === not a palindrome  expected:  false return:  false
-----------
testCASE   A man, a plan, a canal. Panama
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  'a', ' ', 'm', 'a', 'n', ',',
  ' ', 'a', ' ', 'p', 'l', 'a',
  'n', ',', ' ', 'a', ' ', 'c',
  'a', 'n', 'a', 'l', '.', ' ',
  'p', 'a', 'n', 'a', 'm', 'a'
]
for string === A man, a plan, a canal. Panama  expected:  true return:  false
-----------
testCASE   never odd or even
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  'n', 'e', 'v', 'e', 'r',
  ' ', 'o', 'd', 'd', ' ',
  'o', 'r', ' ', 'e', 'v',
  'e', 'n'
]
for string === never odd or even  expected:  true return:  false
-----------
testCASE   nope
toRemove value after regexp:  /\w+/
removed value after slicing:  [ 'n', 'o', 'p', 'e' ]
for string === nope  expected:  false return:  false
-----------
testCASE   almostomla
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  'a', 'l', 'm', 'o',
  's', 't', 'o', 'm',
  'l', 'a'
]
for string === almostomla  expected:  false return:  false
-----------
testCASE   My age is 0, 0 si ega ym.
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  'm', 'y', ' ', 'a', 'g', 'e',
  ' ', 'i', 's', ' ', '0', ',',
  ' ', '0', ' ', 's', 'i', ' ',
  'e', 'g', 'a', ' ', 'y', 'm',
  '.'
]
for string === My age is 0, 0 si ega ym.  expected:  true return:  false
-----------
testCASE   1 eye for of 1 eye.
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  '1', ' ', 'e', 'y', 'e',
  ' ', 'f', 'o', 'r', ' ',
  'o', 'f', ' ', '1', ' ',
  'e', 'y', 'e', '.'
]
for string === 1 eye for of 1 eye.  expected:  false return:  false
-----------
testCASE   0_0 (: /-\ :) 0-0
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  '0', '_', '0', ' ', '(',
  ':', ' ', '/', '-', '\\',
  ' ', ':', ')', ' ', '0',
  '-', '0'
]
for string === 0_0 (: /-\ :) 0-0  expected:  true return:  false
-----------
testCASE   five|\_/|four
toRemove value after regexp:  /\w+/
removed value after slicing:  [
  'f', 'i',  'v', 'e',
  '|', '\\', '_', '/',
  '|', 'f',  'o', 'u',
  'r'
]
for string === five|\_/|four  expected:  false return:  false
-----------
*/

So it’s still hasn’t removed the whitespace?

According to tests, no. I guess it doesn’t work as expected. And as said, consider research about slice.

the symbols are still there too. okay

Slice is removing the values like it should though?

to actually be able to see whats going on in the test cases is very helpful thank you

It doesn’t seem to like the .match() either although it’s been used in a previous test and works fine :expressionless:

PREVIOUS TEST

let quoteSample = "Blueberry 3.141592653s are delicious.";
let myRegex = /[h-s2-6]/ig; // Change this line
let result = quoteSample.match(myRegex); // Change this line
console.log(result);

MY CODE

function palindrome(str) {
  let newStr = str.toLowerCase("");
  let newStrArr = newStr.split("");
  
//* CYCLES THROUGH ARRAY 
  for (let i=0; i < newStrArr.length; i++) {
//* RETURNS ALPHANUMERIC VALUES ONLY
    let toRemove = /[a-z0-9]/;
    let removed = newStrArr.match(toRemove);
console.log(removed);
//* CONCATENATES BACK TO STRING
    let removedJoin = removed.join("");
console.log(removedJoin);
//* REVERSES (let removed) AND STORES IN NEW VARIABLE
    let removedReverse = removed.reverse("");

//* CONCATENATES REVERSED ARRAY TO STRING
    let removedReverseJoin = removedReverse.join("");

//*CONDITIONAL STATEMENT/RETURNS BOOLEAN    
    
    if (removedJoin === removedReverseJoin) {
    return true;
  }
  return false;  
 }
}      

palindrome("eye");

that was just an example of an earlier challenge.

I’m about to delete the for loop then because it seems unnecessary when the regex can work with a string

DONE! :slight_smile:

function palindrome(str) {
  let newStr = str.toLowerCase(""); 
  
//* RETURNS ALPHANUMERIC VALUES ONLY
    let toRemove = /[a-z0-9]/g;
    let removed = newStr.match(toRemove);
console.log(removed);
    let removedJoin = removed.join("");
    
//* REVERSES (let removed) AND STORES IN NEW VARIABLE
    let removedReverse = removed.reverse("");

//*CONCATENATES ARRAY TO STRING
    let removedReverseJoin = removedReverse.join("");

//*CONDITIONAL STATEMENT/RETURNS BOOLEAN    
    
    if (removedJoin == removedReverseJoin) {
    return true;
  }
  return false;  
 }

Thank you thats been bugging me for a while

Yeah, I used similar testing today to solve cash register certification project, that’s helpful indeed.
Not sure if I am doing tests in the most efficient way tho, I’m kinda new to it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.