javascript - How to find digits of a number scaled to their positions? -
javascript - How to find digits of a number scaled to their positions? -
is there improve way find digits of number scaled position?
for example:
'53'
→ ['50', '3']
'123'
→ ['100', '20', '3']
here's i've tried far:
function generatecc(x) { var first_c = x.charat(0); for(var i=0;i<x.length-1;i++) first_c += '0'; var second_c = x-first_c; homecoming [first_c, second_c]; }
function digit(num, n) { homecoming math.floor(num / (math.pow(10, n)) % 10 }
digit(53, 0) returns 3 digit(53, 1) returns 5
you instead convert string , access individual digits using string[]
var res = []; var num = 53; var str = num.tostring(); (var = 0; < str.length; i++){ res.push( parseint(str[i]) * math.pow (10, str.length - - 1) ); }
res [ 50, 3 ]
javascript
Comments
Post a Comment