Palindrome Checker (old course) - cleaning up the input string

I suggest if there is a way this thread is hidden from my fellow learners do so. This is due to the code being almost complete. I am wonder whether the way used in this code to clean the string is close to a correct way or should I try something else. Apart from a lot of unnecessary lines the rest of code should be complete.
HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Palindrome Checker</title>
  <link rel="stylesheet" href="styles.css" type="text"> 
</head>
<body>
  <main>
      <p>Enter text to see if it is a palindrome.</p>
      <input id="text-input" type="text"> 
     <button id="check-btn">
       Check
     </button>
     <div id="result"></div>
  </main>
  <script src="script.js"></script>
</body>
</html>

JavaScript

let textInput=document?.getElementById('text-input');
let checkButton=document?.getElementById('check-btn');
let output=document?.querySelector("#result");

function palindromeCheck(){
  let textInputContent=textInput.value;
  if (textInputContent===""){
    alert("Please input a value");
  }
  let cleanedString=reformat(textInputContent);
  console.log("cleaned string is  " + cleanedString);
  let j=Math.floor(cleanedString.length)-1;
  if (j===0){
    output.innerText=textInputContent + " is a palindrome";
    return;
  }
  for (let i=0; i<Math.floor(cleanedString.length/2); i++) {
    console.log("for 1 entered");
    console.log(cleanedString[i])
    console.log(cleanedString[j])
    if(cleanedString[i]!==cleanedString[j]){
      console.log("if 1 entered");
      output.innerText=textInputContent + " is not a palindrome";
      return ;
    }
    j--;
    if(i===Math.floor(textInputContent.length/2)-1){
    //console.log("if 1 entered");
    output.innerText=textInputContent + " is a palindrome";
      return ;
    }
  }
}
function reformat(stringInput){
  stringInput=stringInput.toLowerCase();
  console.log(stringInput);
  let outputString='';
  let alphaNumeric=
  [0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
  //console.log(alphaNumeric);
  let j=0;
  for(let i in stringInput){
    console.log(stringInput[i]);
    if (stringInput[i] in alphaNumeric){
       console.log('test' + stringInput[i]);
       outputString+=stringInput[i];
       j++;
    }
  }

  //const regex=/[\s]/g;
  //return stringInput.replace(regex, "");
  //return outputString;
  return stringInput;
}
checkButton.addEventListener("click", palindromeCheck);

CSS file is empty hence not included.
If you could tell why colour is showing in my HTML code but not JavaScript please let me know what I am doing wrong.

Hi.

Can you please link to the challenge you are doing.

Does your code pass the tests? If not please say what tests are not passing.

Your javascript code is correctly formatted with 3 backticks. I’m not sure why it’s black and white.

It is Build a Palindrome checker (1st certification project) from JavaScript Algorithms and Data Structures. All test after 4th one fail. If I change the reformat functions return changed to stringInput only 7, 8, 10, 11, 14 and 16 fail. For clarity I know why the test fails just not how to solve it.

I think I have found a way to complete the project I will confirm in a bit.

I have been able to complete the project, I happy to provide pointers to how I managed to complete should think I should. What I will stay it is based on the code shown above but added to a fair bit added to make this approach work. I should note there are other mistakes in program above outside of the cleaning of the string bit.
If not clear from the start of post I approve of locking this thread.

1 Like

You can put html or js right after the first set of triple backticks to enable highlighting for that language:

\    ```html
       <html></html>
\    ```

You can follow these instructions and the site will copy your code and a link to the problem you are working on next time:

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

you could make everything a string, I am not sure in treates string digits and actual numbers the same way

1 Like

Yes that if command was changed in the signed off program. I did it with a if block with function condition. However I tried the way you suggested (at least my understanding of it and got it to work and quickly. I think I prefer that approach 6 lines of code compared to 16 and the positives than come with this. However I will probably keep just my original version when submitting as my own creation and advantages of this approach can be given while they are weaker.