Convert number to a reversed array of digits in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

<p>Given a non-negative integer, we are required to write a function that returns an array containing a list of independent digits in reverse order.</p><h2>For example:</h2><pre class="result notranslate">348597 =&gt; The correct solution should be [7,9,5,8,4,3]</pre><p>The code for this will be &minus;</p><pre class="prettyprint notranslate">const num = 348597; const reverseArrify = num =&gt; { &nbsp; &nbsp;const numArr = String(num).split(&#39;&#39;); &nbsp; &nbsp;const reversed = []; &nbsp; &nbsp;for(let i = numArr.length - 1; i &gt;= 0; i--){ &nbsp; &nbsp; &nbsp; reversed[i] = +numArr.shift(); &nbsp; &nbsp;}; &nbsp; &nbsp;return reversed; }; console.log(reverseArrify(num));</pre><p>Following is the output on console &minus;</p><pre class="result notranslate">[ 7, 9, 5, 8, 4, 3 ]</pre>
raja
Updated on 09-Oct-2020 11:20:15

Advertisements