Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Usually, lines of numeric data must be properly aligned on a screen or page for

ID: 3813096 • Letter: U

Question

Usually, lines of numeric data must be properly aligned on a screen or page for easy viewing, Write a JavaScript function called zeroLead(value, totalLength) that accepts two parameters as follows:

An arbitrary numeric value (for simplicity, you can assume it's an integer.. i.e, no decimal point)

The total length of the output (see example)

The function should return a new string

The total length of the new string is equal to the totalLength provided

If the desired "total length" is less than the length of the input value, then just the input string is returned.

NOTE: This function should not display any results itself, but only return the proper string.

For example:

A call to zeroLead (1234, 7) would return:   0001234

A call to zeroLead (999, 3) would return:   999

A call to zeroLead (12345, 3) would return:   12345

NOTE: You only need to show the function itself... not a complete web page.

Explanation / Answer

function zeroLead(value, totalLength) {
   var valueInString = value.toString();
var noOfDigits = valueInString.length;
var loop = 0;
if(noOfDigits < totalLength){
   loop = totalLength - noOfDigits;
for(i = 1; i <= loop; i++){
   valueInString = "0".concat(valueInString);
}
}   
return valueInString;
}