May I have a hint on this?

I am doing console.log() in my computer to test it, and comes out with true, and false, and with the written values so that I can see…

It comes good for what I think a palindrome should be… but I haven’t been able to make it work on the FCC website…

Here is my computer code:

function palindrome(str) {
const value1 = str.replace(/\s/g, “”);
const value2 = value1.split(“”).reverse().join(“”);

value2 === value1 ? console.log(“true”) : console.log(“false”);
console.log(value2);
}

palindrome(“_eye”);

Console says:

false
eye_

Now for the FCC website I would need to change the code to something like this?

value2 === value1 ? true : false ;

Also… this…

Any hints on what I need to do to change the code?

Do I need to remove characters too?

I have updated your post to include a direct link to the challenge. It’s helpful if you can include this as it makes it easier for others to assist.

One key point in the instructions:

Note: You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.

Your replace is only removing spaces, so you’ll need to modify it.

2 Likes

Two things:

  • You are not returning anything from the function yet, so the tests see palindrome("eye") is undefined.
  • You’ll need to account for:

You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols)

3 Likes

Here is the new code… I think its working from my machine…

Removing spaces and special characters separately because couldn’t figured out how to do them at once… also going to lower case to remove upper case letters…

I think the problem is now in the “return” so the FCC website would like it…

function palindrome(str) {
const value1 = str.replace(/[^a-z]+/gi, “”).toLowerCase();
const value2 = value1.replace(/\d/g, “”);
const value3 = value2.split(“”).reverse().join(“”);

value2 === value3 ? console.log(“true”) : console.log(“false”);
console.log(value2);
}

palindrome(“My age is 0, 0 si ega ym”);

The console says:

true
myageissiegaym

You are correct, yes.

Here’s a hint for you:

What does value2 === value3 evaluate to?

2 Likes

I got the return part of the code working with if else…

but I need to be able to leave letters and number while taking away spaces and characters… that is going to take a while before I figured out…

I got it working…

I needed to do the underscore separately because \w still includes underscores…

Making the return work for the FCC was a bit silly… why can’t they just take my code?

Anyway… its working on the FCC site… below is my computers code when I was testing it…

function palindrome(str) {
const value1 = str.replace(/_+/g, “”).toLowerCase();
const value2 = value1.replace(/[^\w]+/g, “”);
const value3 = value2.split(“”).reverse().join(“”);

value2 === value3 ? console.log(“true”) : console.log(“false”);
console.log(value1);
console.log(value2);
console.log(value3);

}

palindrome(“_eye”);

1 Like

Can I ask what you mean by “making the return work for FCC was a bit silly”? Looking at the code you just shared, I don’t see a return statement - just as @nhcarrigan mentioned above. You are only logging variables to the console, rather than returning them from your function, which is required for evaluation by FCC.

I’m glad you got it working, but I’m unsure what problem is being caused by submitting your code on this site in this situation.

3 Likes

For example this line of code was no good the FCC website, but did work from my machine:

value2 === value3 ? console.log(“true”) : console.log(“false”);

I needed to change the code to if () {} else{} structure to make it match properly.

You didn’t need to change it to an if else statement.
Ternary works here but you have to return the value which is what is missing

This code works

function palindrome(str) {
  const value1 = str.replace(/_+/g, "").toLowerCase();
  const value2 = value1.replace(/[^\w]+/g, "");
  const value3 = value2.split("").reverse().join("");

  return value2 === value3 ? true : false;

}

palindrome("eye");

You can also get rid of the ternary all together and just return the result of using the strict equality operator since that would return a true false anyway.

This works too

function palindrome(str) {
  const value1 = str.replace(/_+/g, "").toLowerCase();
  const value2 = value1.replace(/[^\w]+/g, "");
  const value3 = value2.split("").reverse().join("");

  return value2 === value3;

}

palindrome("eye");
1 Like

There is nothing silly about requiring a function to actually produce a value or have a side-effect. If a function does not return anything or alter outside variables, it isn’t doing anything.

Logging by itself does nothing and it has zero value in an application other than for the developer to look at values. You do not ask your users to open the browser console so they can use your app.

4 Likes

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