Digit value to array in JavaScript -
i have number 8939
, want array [8000,7000,6000,5000,4000,3000,2000,1000]
, example if have 340
can array 300,200,100
.
i knew , if user
i = i/1000 * 1000
then can round down 8939 8000, don't know how can got result want above.
no reason complicate it, can use for
loop
function getarrfornum(num){ var arr = []; // create new array var digitnum = num.tostring().length; //get number of digits, can use math.log instead var mulof10 = math.pow(10,digitnum-1); for(var i=1;i*mulof10 < num;i++){ arr.push(i*mulof10); // push next multiple of 1000 } return arr; //return }
then can use it:
getarrfornum(3211); //1000,2000,3000 getarrfornum(321); //100,200,300
here 1 line version challenge, suggest avoiding :)
array(+(832+"")[0]).join(".").split(".").map(function(_,i){ return (i+1)*math.pow(10,(832+"").length-1);}); // 800,700,600,500,400,300,200,100 array(+(3332+"")[0]).join(".").split(".").map(function(_,i){ return (i+1)*math.pow(10,(3332+"").length-1);}); //1000,2000,3000
Comments
Post a Comment