{"id":329,"date":"2023-08-06T10:56:59","date_gmt":"2023-08-06T16:56:59","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=329"},"modified":"2023-08-11T11:41:34","modified_gmt":"2023-08-11T17:41:34","slug":"identify","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/identify\/","title":{"rendered":"Working with digits of an integer in javascript (no array iterators)."},"content":{"rendered":"\n<p>Given an integer n, how to identify each of the digits that are are part of n. (Example, n = 479, got 4 separately, 7 separately and 9 separately to work with them).<\/p>\n\n\n\n<p>A solution in languages like c++ or even c, could be found by using a bitwise AND operator and make a bit to bit masking, but in javascript, things work a bit different, so this solution considers low level algorithm using modules and power of 10, since we are working with decimas. (and no use of array iterators).<\/p>\n\n\n\n<p>taking the 1st power of 10, we obtain 10, also 10 to the power of 0, gives us 1, so if compute n module of each of those numbers ( where n &gt; 0 and integer), we obtain the residue, and substracting, and dividing, we obtain the number required.<\/p>\n\n\n\n<p>The following code, considers a regular for loop, from 0 to the size of n, with increments of 1.<\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#abb8c3\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-php\">const n = 479;\nlet x = 0;\nx = ( (n % Math.pow(10, i+1)) - (n % Math.pow(10, i) ) \/ Math.pow(10,i);<\/code><\/pre><\/div><\/div>\n\n\n\n<p>Now, wen can actually use each digit to calculate the module in which the original number n, is divisible by the resultant digit, like this: <\/p>\n\n\n\n<div class=\"dm-code-snippet dark dm-normal-version default no-background-mobile\" snippet-height=\"\" style=\"background-color:#5E69FF\"><div class=\"control-language\"><div class=\"dm-buttons\"><div class=\"dm-buttons-left\"><div class=\"dm-button-snippet red-button\"><\/div><div class=\"dm-button-snippet orange-button\"><\/div><div class=\"dm-button-snippet green-button\"><\/div><\/div><div class=\"dm-buttons-right\"><a id=\"dm-copy-raw-code\"><span class=\"dm-copy-text\">Copy Code<\/span><span class=\"dm-copy-confirmed\" style=\"display:none\">Copied<\/span><span class=\"dm-error-message\" style=\"display:none\">Use a different Browser<\/span><\/a><\/div><\/div><pre class=\" line-numbers\"><code id=\"dm-code-raw\" class=\" no-wrap language-javascript\">function digitsDivisible(n) {\n    let size1 = n.toString().length;\n    let count = 0;\n    let x = 0;\n    for (let i = 0; i &lt; size1; i++) {\n        x = (((n % Math.pow(10, i+1)) - (n % Math.pow(10, i)))\/Math.pow(10,i));\n        if ( x > 0) {\n            if (n % x == 0) {\n                count++;\n            }\n        }\n    }\n    \n    return count;\n}<\/code><\/pre><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer n, how to identify each of the digits that are are part of n. (Example, n = 479, got 4 separately, 7 separately and 9 separately to work with them). A solution in languages like c++ or even c, could be found by using a bitwise AND operator and make a bit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":243,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[36,35,7],"tags":[],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/329"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":5,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"predecessor-version":[{"id":380,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/329\/revisions\/380"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/243"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}