-
-
Save lassbon/4907230d53d7c7b4e339dfd013f664e4 to your computer and use it in GitHub Desktop.
Number is a palindrome if it is equal to the number with digits in reversed order. For example, 5, 44, 171, 4884 are palindromes and 43, 194, 4773 are not palindromes. Write a method palindrome_chain_length which takes a positive number and returns the number of special steps needed to obtain a palindrome. The special step is: "reverse the digit…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var palindromeChainLength = function(n) { | |
| let notIsPalindrome = true, index = 0, value=n; | |
| while(notIsPalindrome){ | |
| let reversed = Number.parseInt(value.toString().split('').reverse().join('')); | |
| if(value == reversed){ | |
| notIsPalindrome = false; | |
| }else{ | |
| index++; | |
| value += reversed; | |
| } | |
| } | |
| return index; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment