4

I have an input field that expects a 10 digit number. If the user enters and submits a number less than 10 digits, the function would simply add a "0" until the inputed value is 10 digits in length.

I haven't really used, or understand how recursive functions really work, but I'm basically looking at an efficient way of doing this. One minor issue I'm having is figuring out how to prepend the "0"s at the beginning of the string rather than appended to the end.

My thinking:

function lengthCheck(sQuery) {
    for (var i = 0; i < sQuery.length; i++) {
        if (sQuery.length !== 10) {
            sQuery += "0";
            //I'd like to add the 0s to the beggining of the sQuery string.
            console.log(sQuery);
            lengthCheck(sQuery);
        } else return sQuery
    }
}
Félix Gagnon-Grenier
  • 7,344
  • 10
  • 45
  • 58
Kode_12
  • 3,002
  • 7
  • 30
  • 72
  • Wow, completely overlooked such a simple syntax, thanks – Kode_12 Oct 27 '16 at 17:32
  • 1
    Lots of good answers here: http://stackoverflow.com/questions/5366849/convert-1-to-0001-in-javascript – Jeff McCloud Oct 27 '16 at 17:40
  • Make sure you watch out for type coercion which might influence number conversions when using padded zero numbers in JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates For instance: Decimal literals can start with a zero (0) followed by another decimal digit, but If the next digit after the leading 0 is smaller than 8, the number gets parsed as an octal number. – Benjamin Dean Oct 27 '16 at 17:47

5 Answers5

5

Change:

sQuery += "0"; // added at end of string

to:

sQuery = "0" + sQuery; // added at start of string

To remove the for loop/recursion, you could slice out the desired length in one step:

function padZeros(sQuery) {
    // the max amount of zeros you want to lead with
    const maxLengthZeros = "0000000000"; 

    // takes the 10 rightmost characters and outputs them in a new string
    return (maxLengthZeros + sQuery).slice(-10); 
}

Simple generic function using ES6 repeat:

// edge case constraints not implemented for brevity
function padZeros(sQuery = "", maxPadding = 10, outputLength = 10) {
  // the max amount of zeros you want to lead with
  const maxLengthZeros = "0".repeat(maxPadding); 

  // returns the "outputLength" rightmost characters
  return (maxLengthZeros + sQuery).slice(-outputLength); 
}

console.log('padZeros: ' + padZeros("1234567890"));
console.log('padZeros: ' + padZeros("123"));
console.log('padZeros: ' + padZeros(""));

Alternate version that doesn't affect strings over your set limit:

function padZerosIfShort(inputString = "", paddedOutputLength = 10) {        
  let inputLen = inputString.length;
  
  // only padded if under set length, otherwise returned untouched
  return (paddedOutputLength > inputLen)
    ? "0".repeat(paddedOutputLength - inputLen) + inputString 
    : inputString;                                            
}

console.log('padZerosIfShort: ' + padZerosIfShort("1234567890", 5));
console.log('padZerosIfShort: ' + padZerosIfShort("123", 5));
console.log('padZerosIfShort: ' + padZerosIfShort("", 5));

It will ultimately depend on your needs how you want to implement this behavior.

Jecoms
  • 1,967
  • 2
  • 17
  • 24
2

The += operator adds things to the end of strings similar to:

sQuery=sQuery+"0"

You can add characters to the front of a string like this

sQuery="0"+sQuery

I also found something interesting here. it works like this:

("00000" + sQuery).slice(-5)

You would add zeros to the front then slice off everything except the last 5. so to get 10 characters you would use:

("0000000000" + n).slice(-10)
Community
  • 1
  • 1
Buzz
  • 1,751
  • 20
  • 24
1

You don't need recursion to solve this, just a simple for loop should do the trick. Try this:

function lengthCheck (sQuery) {
    for (var i = sQuery.length; i<10; i++) {
        sQuery = "0" + sQuery;  
    }
    return sQuery;
}
Andrew D
  • 209
  • 1
  • 6
1

You're looking to pad the string with zeroes. This is an example I've used before from here and will shorten your code a little bit:

function lengthCheck (sQuery) {
    while (sQuery.length < 10)
        sQuery = 0 + sQuery;
    return sQuery;
}
Community
  • 1
  • 1
Ding
  • 2,987
  • 1
  • 14
  • 26
0

I believe this has already been answered here (or similar enough to provide you the solution): How to output integers with leading zeros in JavaScript

Community
  • 1
  • 1
Benjamin Dean
  • 1,000
  • 9
  • 11