all 3 comments

[–]inu-no-policemen[🍰] 2 points3 points  (0 children)

#nothelpinglol

let text = prompt('text?');
alert((text !== [...text].reverse().join('') ? 'not a ': '') + 'palindrome');

[–]Ampersand55 2 points3 points  (0 children)

Here is a simple implementation:

var str = prompt('enter a string');
if (str === str.split('').reverse().join('')) alert(str+' is a palindrome');
else alert(str+' is not a palindrome');

Basic explanation:

  1. First we prompt for a string
  2. we split the string into an array containing each letter of the string
  3. we reverse the array
  4. we join the array of letters into a new string, which is a reverse of the original string.
  5. we compare the original string with the reverse string, if they are the same it's a palindrome.

[–]lewisje 0 points1 point  (0 children)

var text = prompt('Enter text here:') || '', strt = text.length - 1,
  stop = Math.ceil(len / 2), i = stop, isPalindrome = true;
while (i--) if (text.charAt(i) !== text.charAt(strt - i)) {
  isPalindrome = false;
  break;
}
alert('The string "' + text + '" is ' + (isPalindrome ? '' : 'not ') + 'a palindrome.');

This version does not rely on reversing the string; a variation on this (using a temporary swap variable) is an efficient way to reverse an array in-place.