Checking whether the sum of digits of a number forms a Palindrome Number or not in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

<p>We are required to write a JavaScript function that takes in a number, sums its digits and checks whether that sum is a Palindrome number or not. The function should return true if the sum is Palindrome, false otherwise.</p><p>For example, if the number is 697,</p><p>Then the sum of its digit will be 22, which indeed, is a Palindrome number. Therefore, our function should return true for 697.</p><h2>Example</h2><p>Following is the code &minus;</p><!---<p><a href="" target="_blank" rel="nofollow" class="demo"><i class="fa-external-link"></i> Live Demo</a></p>---><pre class="prettyprint notranslate">const num = 697; const sumDigit = (num, sum = 0) =&gt; { &nbsp; &nbsp;if(num){ &nbsp; &nbsp; &nbsp; return sumDigit(Math.floor(num / 10), sum + (num % 10)); &nbsp; &nbsp;}; &nbsp; &nbsp;return sum; }; const isPalindrome = num =&gt; { &nbsp; &nbsp;const revered = +String(num) &nbsp; &nbsp;.split(&quot;&quot;) &nbsp; &nbsp;.reverse() &nbsp; &nbsp;.join(&quot;&quot;); &nbsp; &nbsp;return revered === num; }; const isSumPalindrome = num =&gt; isPalindrome(sumDigit(num)); console.log(isSumPalindrome(num));</pre><h2>Output</h2><p>This will produce the following output in console &minus;</p><pre class="result notranslate">true</pre>
raja
Updated on 30-Sep-2020 14:16:06

Advertisements