Javascript help. palindrome

I dont know why sending ‘nope’ through this function would return True.

Is there something wrong with my If statement?

  **Your code so far**

function palindrome(str) {
let regex = /\w+/gi;
let newArr = str.match(regex)
let num1 = newArr
.join('')
.toLowerCase(); 

if(num1.length % 2 === 0){
for(let i = 0; i < num1.length / 2; i++){
  if(num1.indexOf(i) === num1.indexOf(num1.length - i)){
    return true
  }else{
    return false
  }
}
}

if(num1.length % 2 === 1){
for(let i = o; i < Math.floor(num1.length / 2); i++){
  if(num1.indexOf(i) === num1.indexOf(num1.length - i)){
    return true
  }else{
    return false
  }
}
}

}

console.log(palindrome("nope"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Challenge: Palindrome Checker

Link to the challenge:

I don’t understand how you intend this function to work. Some clearer variable names and comments would help greatly.

I don’ think this method works the way you think it works.

Your function stops immediately and does nothing else once it encounters a return statement.

Thanks Jeremy for the reply.

Im really bad at this, just trying to learn. Im going to re-evaluate the indexOf methods as i know understand why that isnt the right one to use and change the variable names.

Im outside Philly so i have a bunch of snow coming tonight and tomorow so i will dive into this again on Sunday.

Thanks again for your time to answer Jeremy

Rob

What??? Being snowed in is the perfect excuse to play on your computer all day :smiley:

Made it thorugh the snow. Im in landscape so sadly during the snow i am out for days clearing it.

Also being a landscaper is my excuse for being terrible at javascript. But i swear i am trying my hardest. Below is where i am at now.

function palindrome(str) {

let regex = /[A-Za-z]/gi;

let matchedStr = str.match(regex);

console.log(matchedStr);

let reducedStr = matchedStr

.join('')

.toLowerCase(); 

console.log(reducedStr);

if(reducedStr.length % 2 === 0){

for(let i = 0; i < reducedStr.length / 2; i++){

  if(reducedStr.charAt(i) === reducedStr.charAt(reducedStr.length - i)){

    return true

  }else{

    return false

  }

}

}

if(reducedStr.length % 2 === 1){

for(let i = 0; i <= Math.floor(reducedStr.length / 2); i++){

  if(reducedStr.charAt(i) === reducedStr.charAt(reducedStr.length - i)){

    return true

  }else{

    return false

  }

}

}

}

console.log(palindrome("race car"));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Ahh, that makes sense, you aren’t playing in the snow, you’re working in the snow.

As for your code, you’ve got a lot going on here. I think you should work on simplifying a bit. Let’s work on the first requirement: You’ll need to remove all non-alphanumeric characters.

You started out on the right track, you’ll definitely want to use a regex to do this. You can think of a string as a series of characters. What you want to do is go through the string character by character and remove any non-alphanumeric ones. Looping through the string is one way to go, but wouldn’t it be cool if there was a JS string method that made this much easier? A method that sort of allowed you to replace all of these characters with another character (such as an empty string character). If you replaced all of the non-alphanumeric characters with an empty string wouldn’t that be the same as removing them from the string?

jeremy,
thank you for the help. will make sure i follow correct format.

bbsmooth
thanks for your time and all the help and advice. Ive been having such a hard time with javascriopt. I still have that feeling like i dont have a handle on it yet. LIke im never sure what the next step should be.

I will give this another shot tomorrow

thanks again

rob

I think i have what was mentioned about using replace to reduce the string to only the alphanumeric characters but i still can only get a ‘false’ when the function runs. I know my for loops must be real sloppy but i cant tell what to change or where to go next

function palindrome(str) {
let regex = /[\W+\s]/gi;
let replacedStr = str.replace(regex, '');
console.log(replacedStr);


/*let matchedStr = str.match(regex);
console.log(matchedStr);
let reducedStr = matchedStr
.join('')
.toLowerCase(); 
console.log(reducedStr);
*/

if(replacedStr.length % 2 === 0){
for(let i = 0; i < replacedStr.length / 2; i++){
  if(replacedStr.charAt(i) == replacedStr.charAt(replacedStr.length - i)){
    return true
  }else{
    return false
  }
}
}

if(replacedStr.length % 2 === 1){
for(let i = 0; i <= Math.floor(replacedStr.length / 2); i++){
  if(replacedStr.charAt(i) === replacedStr.charAt(replacedStr.length - i)){
    return true
  }else{
    return false
  }
}
}

}


console.log(palindrome("race car"));

A couple of things are happening. Let’s back up and add more console logs:

function palindrome(str) {
  let regex = /[\W+\s]/gi;
  let replacedStr = str.replace(regex, '');
  console.log(replacedStr);

  for (let i = 0; i < replacedStr.length / 2; i++) {
    console.log("i: " + i);
    console.log("comparing " + replacedStr.charAt(i) + " and " + replacedStr.charAt(replacedStr.length - i));
    if (replacedStr.charAt(i) == replacedStr.charAt(replacedStr.length - i)) {
      return true
    } else {
      return false
    }
  }
}


console.log(palindrome("race car"));

I cut down the repeated code so its easier for us to look, and then we can add back handling of odd vs even later.

What do you see in the console output?

i: 0
comparing r and
false

I think the second part of my ‘compare’ expression isnt working as i intended. am i on the right track?

Yep, that is where I would start looking for the first bug.

What is this value when i=0?

oh snap. it is 7 but should be 6. am i getting closer?

i just changed to (replacedStr.length -1 - i)

Yup, that’s a big step forward. What happens when you fix that?

i ran the function and got a bunch of check marks. im going to work on why the few X’s are still there.

Jeremy, if your ever in south east PA, i owe you a 6 pack.

Ill update shortly with my next conundrum. For some reason i am having a really hard time getting this to come natural.

1 Like

Its a tricky process to learn, but you’re getting there!

i am at the last X. I cant figure out why my for loop isnt continuing all the way to i = 4, which should trigger it to go false.


function palindrome(str) {
  let regex = /[\W+\s_]/gi;
  let replacedStr = str
  .replace(regex, '')
  .toLowerCase();
  console.log(replacedStr);

  for (let i = 4; i <= replacedStr.length / 2; i++) {
    console.log("i: " + i);
    console.log(replacedStr.length -1 - i);
    console.log("comparing " + replacedStr.charAt(i) + " and " + replacedStr.charAt(replacedStr.length -1 - i));
    if (replacedStr.charAt(i) == replacedStr.charAt(replacedStr.length -1 - i)) {
      return true
    } else {
      return false
    }
  }
}


console.log(palindrome("almostomla"));

As soon as a return statement is encountered, your function immediately stops what its doing and exits. So when do you want to return true?

Jeremy

It might be the ugliest code you have ever seen and i probably cant take any credit for it since all the help i got but this code passes.

Thank you Jeremy and bbsmooth

function palindrome(str) {
  let regex = /[\W+\s_]/gi;
  let replacedStr = str
  .replace(regex, '')
  .toLowerCase();
  console.log(replacedStr);
  console.log(Math.floor(replacedStr.length / 2));
  let count = 0;
  

  for (let i = 0; i < Math.floor(replacedStr.length / 2); i++) {
    console.log("i: " + i);
    console.log(replacedStr.length -1 - i);
    console.log("comparing " + replacedStr.charAt(i) + " and " + replacedStr.charAt(replacedStr.length -1 - i));
    if (replacedStr.charAt(i) == replacedStr.charAt(replacedStr.length -1 - i)) {
      count++;
      console.log('Count:' + count);
      if(count == Math.floor(replacedStr.length / 2)){
        return true
      }

    } else {
      return false
    }
  }
}


console.log(palindrome("racecar"));
1 Like