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
Post a Comment