Sum of all N digit palindrome numbers | Function to check palindrome ; Function to calculate the sum of n - digit palindrome ; Run a loop to check all possible palindrome ; If palindrome append sum ; Driver code
function isPalindrome ( s ) { var left = 0 , right = s . length - 1 ; while ( left <= right ) { if ( s [ left ] != s [ right ] ) { return false ; } left ++ ; right -- ; } return true ; } function getSum ( n ) { var start = Math . pow ( 10 , n - 1 ) ; var end = Math . pow ( 10 , n ) - 1 ; var sum = 0 ; for ( var i = start ; i <= end ; i ++ ) { var s = ( i . toString ( ) ) ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } var n = 1 ; var ans = getSum ( n ) ; document . write ( ans + " " ) ;