/ Published in: JavaScript

This JavaScript code snippet checks each integer in an array. If necessary, one or more leading zeros are added to an integer. Note that each integer becomes a string.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* EXAMPLE document.write(addLeadingZeros([1,2,3,4,5,6,7,8,9,10])); */ function addLeadingZeros(array) { var highestInt = Math.max.apply(Math,array); for(var i=0; i<array.length; i++) { var nLeadingZeros = highestInt.toString().length - array[i].toString().length; for(var j=0; j<nLeadingZeros; j++) array[i] = '0' + array[i]; } return array; }
Comments
