How to check if the string is palindrome or not using Javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to check if the string is palindrome or not using Javascript?

19th Oct 2016, 1:56 PM
Abhishek Rajput
Abhishek Rajput - avatar
4 Answers
+ 1
New to coding so a little unsure myself. But my theory is that you could use a loop to count BACKWARDS from the end of the string, comparing each letter to the letter at the opposite end of the string?? **Edit: Neater method using arrays Heres my check: ============ var string = "kayak" var origWord = [] var testPalin = [] var i = 0 var j = string.length -1 while (i < string.length) { origWord.push(string[i]); testPalin.push(string[j]); i++ j-- if (i > 6) { break; } } document.write(origWord + "<br />"); document.write(testPalin + "<br />"); Tested with "kayak" --> Output: ************************************* k,a,y,a,k k,a,y,a,k Tested with "stella" --> Output: ********************************** s,t,e,l,l,a a,l,l,e,t,s
20th Oct 2016, 1:50 PM
Pink Lemonade
0
But what if we don't know the string length?
22nd Oct 2016, 4:31 PM
Abhishek Rajput
Abhishek Rajput - avatar
0
The while loop uses string.length any way, so it will run the loop until the end of the string regardless. Just need to change: var string=prompt("Enter string") ; So that you enter any string of choice
22nd Oct 2016, 5:20 PM
Pink Lemonade