Splitting an integer up in original order and storing it in an array in javascript -


how can done? user enters integer 14865, how can cut integer , put array in exact same order so:[1, 4, 8, 6, 5]. i've tried using %10 method, returns front.

one method turn integer string , use string .split() method create array containing each digit. @ point each element of array string, loop on array turn each array element string number (or use .map()):

var x = 14865,     = x.tostring().split("").map(function(v) { return +v; });  // [1, 4, 8, 6, 5] 

note if integer entered user can skip .tostring() part, because string unless you've explicitly converted number.

p.s. mentioned "the %10 method" returns values "back front" - don't show how implemented method, if current code extracts digits 1 @ time , adds them end of array "back front" why not avoid problem inserting digits @ beginning of array .unshift() method?


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -