Return to Snippet

Revision: 30403
at September 4, 2010 09:24 by DADU


Updated Code
/* 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;
}

Revision: 30402
at August 13, 2010 06:34 by DADU


Initial Code
/* 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;
}

Initial URL

                                

Initial Description
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.

Initial Title
add leading zeros

Initial Tags
javascript, list, array

Initial Language
JavaScript