# Javascript help. palindrome

**URL:** <https://forum.freecodecamp.org/t/javascript-help-palindrome/494659>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 28, 2022, 8:49pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659 "2022-01-28T20:49:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [January 28, 2022, 8:49pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/1 "2022-01-28T20:49:12Z")

</div>

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

Is there something wrong with my If statement?

```
  **Your code so far**

```

```js

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:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [January 28, 2022, 8:54pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/2 "2022-01-28T20:54:19Z")

</div>

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

> [@robbyweeds](#):
>
> `num1.indexOf(i)`

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

> [@robbyweeds](#):
>
> ` return true`

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

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [January 28, 2022, 11:38pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/3 "2022-01-28T23:38:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [January 28, 2022, 11:40pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/4 "2022-01-28T23:40:39Z")

</div>

> [@robbyweeds](#):
>
> Im outside Philly so i have a bunch of snow coming tonight and tomorow so i will dive into this again on Sunday.

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

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 3, 2022, 7:57pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/5 "2022-02-03T19:57:08Z")

</div>

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.

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 3, 2022, 8:11pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/6 "2022-02-03T20:11:41Z")

</div>

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.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [February 3, 2022, 8:20pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/7 "2022-02-03T20:20:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 4, 2022, 1:41am UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/8 "2022-02-04T01:41:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 7, 2022, 7:10pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/9 "2022-02-07T19:10:00Z")

</div>

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

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 7, 2022, 7:14pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/10 "2022-02-07T19:14:35Z")

</div>

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

```javascript
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?

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 7, 2022, 7:58pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/11 "2022-02-07T19:58:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 7, 2022, 8:01pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/12 "2022-02-07T20:01:32Z")

</div>

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

> [@JeremyLT](#):
>
> `replacedStr.length - i`

What is this value when `i=0`?

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 7, 2022, 8:04pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/13 "2022-02-07T20:04:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 7, 2022, 8:05pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/14 "2022-02-07T20:05:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 7, 2022, 8:05pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/15 "2022-02-07T20:05:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 7, 2022, 8:08pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/16 "2022-02-07T20:08:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 7, 2022, 8:09pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/17 "2022-02-07T20:09:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 7, 2022, 8:17pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/18 "2022-02-07T20:17:42Z")

</div>

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.

```auto

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"));

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 7, 2022, 8:20pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/19 "2022-02-07T20:20:20Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![robbyweeds](https://avatars.discourse-cdn.com/v4/letter/r/b38774/32.png) [@robbyweeds](https://forum.freecodecamp.org/u/robbyweeds)\
**Post date:** [February 7, 2022, 8:45pm UTC](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659/20 "2022-02-07T20:45:51Z")

</div>

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

```auto
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"));

```

[Next page](https://forum.freecodecamp.org/t/javascript-help-palindrome/494659.md?page=2)
