-1

So I am trying to create a code for palindrome this is how I tried. Is there another or better way of doing it?

But now it only shows that if the first value is equal or not and shows true or false?

var inpo= prompt("Please enter to check if palindrome")
var inp = parseFloat(inpo)
var a = inpo.split('')
var inpo2 = a.reverse()
var len= inpo.length

for (var i =0;i< len ;i++) { 
  if (inpo[i] == inpo2[i] )
    alert("True")
  else
    alert("False")
}
derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • What is the input you are providing? – Harmandeep Singh Kalsi Aug 27 '20 at 17:32
  • 3
    [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask): Write a title that summarizes the _problem_, introduce the problem before you post any code, add a [mcve] that reproduces the problem. And don't use unnecessary formatting (e.g. write everything in your question in bold) – Andreas Aug 27 '20 at 17:34
  • There are a bunch of issues here. Why use parseFloat()? You're not using that value again. You're checking each letter (aside from the middle one if the string has odd length) twice. That's unnecessary. Instead of reversing the string, just compare two letters at different positions in the original string. – Robby the Belgian Aug 27 '20 at 17:40

3 Answers3

2

If you are creating a palindrome checker code, here is a simple way to do it. Split, reverse then join.

str1 = "xxaa";
str2 = str1.split('').reverse().join("");

if (str1 == str2) {
  alert("good");
} else {
  alert("not");
}
imvain2
  • 15,480
  • 1
  • 16
  • 21
  • 1
    If you're just going to post an alternative solution then choose one of the many possible duplicates and vote to close. – Andreas Aug 27 '20 at 17:46
2

A way to check if a word or an entire phrase is a palindrome or not:

function isPalindrome(str) {
    // Escape the string: Eliminate punctuation and spaces, enforce lower case
    let escaped = str.replace(/[^A-Za-z0-9_]/g,"").toLowerCase();
    
    // Reverse the escaped string
    let reversed = escaped.split('').reverse().join('');
    
    //compare
    return escaped == reversed;
}

console.log(isPalindrome('Level'));
console.log(isPalindrome('Red rum, sir, is murder'));

I hope the comments serve well as an explanation.

Also, you have a prompt example in THIS jsfiddle.

Thor
  • 3
  • 1
Razvan Zamfir
  • 4,209
  • 6
  • 38
  • 252
1

You can check the single character from the string consuming 1 character from right and another from left until you will find either the string is finished or there are 2 inequal character. I implemented with a classical for loop. Note that bracket notation for strings [] is only recently supported, you can use charAt if memory serves me right

let inp = "otdto";

console.log(isPalindrome(inp));

function isPalindrome(inp) {
  const len = inp.length;
  for (let i = 0; i < Math.floor(len / 2); i++)
    if (inp[i] != inp[len - i - 1])
      return false;

  return true;
}
Terry
  • 63,248
  • 15
  • 96
  • 118
Nick
  • 1,439
  • 2
  • 15
  • 28
  • 1
    _"Note that bracket notation for strings [] is only recently supported"_ - This feature is part of ECMAScript 5 which has been published on [December 3, 2009](https://en.wikipedia.org/wiki/ECMAScript#5th_Edition) – Andreas Aug 27 '20 at 17:49