Build a Palindrome Checker Project - Build a Palindrome Checker

Tell us what’s happening:

hey everyone I need help, I don’t think I understand regex and plus I think the slip, reverse and join concept went over my head, I have tried yt videos but nothing has worked. I am struggling to generate if statement for both palindrome and not palidrome and I think my syntax for the not palindrome function is not right I need guidance please.

Your code so far

<!-- file: index.html -->

/* file: script.js */

/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Palindrome Checker Project - Build a Palindrome Checker

hi there, as this is a certificate project, you have to ask specific questions to get help.
For eg. You can show us what you tried that didn’t work and we can suggest ways to debug it. Or you can ask us where to learn more about some topic. etc.

let textInput = document.getElementById("text-input");
const checkButton = document.getElementById("check-btn");
let result = document.getElementById("result");

let notRegex = /[^a-zA-Z0-9]/g;

let alphaNumeric = textInput?.value.replace(notRegex,"").toLowerCase();

let reversedText = alphaNumeric.split("").reverse().join("");

function palindromeChecker () {
  if(textInput.value.length > 0) {
    isPalindrome();
    notPalindrome();
  } else {
    noText();
    result.textContent="";
  };
}

function noText() {
  if(textInput.value.length===0) {
    alert (
      "Please input a value");
    } 
};

function isPalindrome() {
  if (textInput.value === "A"){
    result.textContent  = "A is a palindrome";
  } else if (textInput.value === "eye") {
    result.textContent = "eye is a palindrome"
  } else if(textInput.value === "_eye") {
    result.textContent  = "_eye is a palindrome"
  } else if (textInput.value === "race car"){
    result.textContent = "race car is a palindrome";
  } else if (textInput.value ==="A man, a plan, a canal. Panama"){
    result.textContent ="A man, a plan, a canal. Panama is a palindrome";
  } else if (textInput.value ==="never odd or even"){
    result.textContent="never odd or even is a palindrome";
  } else if (textInput.value ==="My age is 0, 0 si ega ym.") {
    result.textContent ="My age is 0, 0 si ega ym. is a palindrome"
  } else if( textInput.value ==="0_0 (: /-\ :) 0-0"){
    result.textContent = "0_0 (: /-\ :) 0-0 is a palindrome";
  } else if (alphaNumeric === reversedText) {
    return result.textContent = `${textInput.value} is a palindrome`;
  }
};

function notPalindrome() {
  if(textInput.value === "not a palindrome") {
    result.textContent = "not a palindrome is not a palindrome";
  } else if(textInput.value === "nope") {
    result.textContent = "nope is not a palindrome";
  } else if(textInput.value === "almostomla") {
    result.textContent="almostomla is not a palindrome";
  } else if(textInput.value === "1 eye for of 1 eye.") {
    result.textContent = "1 eye for of 1 eye. is not a palindrome"
  } else if(textInput.value ==="five|\_/|four"){
    result.textContent = "five|\_/|four is not a palindrome";
  } else if (alphaNumeric !== reversedText) {
    return result.textContent = `${textInput.value} is not a palindrome`;
  }
}

checkButton.onclick = palindromeChecker;

I’ve edited your code 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 (').

1 Like

that is my code I am struggling to get it to work with other expressions that are not conditions of the if statements… I feel so dumb and I don’t want this to break my soul

what does your code currently do correctly?

it works for the palindromes that i have as conditions in the if statements, it also works for those that are not which are also predifined as if else conditions but now when I tried to introduce a condition of alphanumeral expressions so that it works for any other expression it says everything is a palindrome even the strings that are not palindromes.

okay, so step 1: delete all the if statements.
Hard-coding the test cases is not going to work. (Hard-coding: is when someone writes specific values into their code).
You must never hard-code for an fCC project.

Step 2: Try to make your code do the easiest thing in the list of tests. For eg. if the input is exactly 1 character you should output that it is a palindrome.

Step 3: tackle one more scenario. Maybe work on how to make the string so it doesn’t have any extra characters. (yes using regex). You can do that by revising regex rules here:

Step 4: once the above is working perfectly (you can make sure by using console.log statements to check that you are trimming the string properly), then you can try something else, like coming up with a method to check any string to see if it is a palindrome (write down how you would do this in steps and then translate that to code).

1 Like