Skip to content

Instantly share code, notes, and snippets.

@jluismm2311
Created September 14, 2015 20:35
Show Gist options
  • Select an option

  • Save jluismm2311/529612fd4b550f860380 to your computer and use it in GitHub Desktop.

Select an option

Save jluismm2311/529612fd4b550f860380 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…
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;
}
@Gabude
Copy link
Copy Markdown

Gabude commented Aug 3, 2022

thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment