Home > Net >  Matching number in a string including leading zeros
Matching number in a string including leading zeros

Time:12-05

i am doing this exercise:

Write a function which increments a string, to create a new string. If the string already ends with a number, the number should be incremented by 1. If the string does not end with a number. the number 1 should be appended to the new string.

And this is the part where i am stuck

If the number has leading zeros the amount of digits should be considered.

This is what i did.


sumStrings = (a) => {
    
    if (a.match(/\d /g) == null) {
        console.log(a.match(/[a-z] /g)   "1")
    } else {
        let numbers = a.match(/\d /g).map(Number);
        console.log(a.match(/[a-z] /g)   (Number(numbers.join()) 1))
    }
}

sumStrings("hello002")

returns hello3

Why doesn't match the leading zeros?

CodePudding user response:

By calling map(Number) you cast the string to a Number-type. The number does only contain it's value.

CodePudding user response:

You have to first get the leading zeros from the string, then add them back later once you've incremented the number. Data of Number type will never show leading zeros -- "002" as a number will always be 2 - so you have to manipulate the string first.

This line tests for leading zeros, and will default to an empty string if they're not there.

let leadingz = a.replace(/[a-z] /g, '').match(/^0 /)?.join('') || '';

const sumStrings = a => {
  if (a.match(/\d /g) == null) {
    console.log(a.match(/[a-z] /g)   "1")
  } else {
    let leadingz = a.replace(/[a-z] /g, '').match(/^0 /)?.join('') || '';
    let numbers = a.match(/\d /g).map(Number);
    console.log(a.match(/[a-z] /g)   leadingz   (Number(numbers.join())   1))
  }
}

sumStrings("hello000002")
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Here is a solution that preserves the minimum number of digits:

sumStrings = (a) => {
    let num = a.match(/\d /);
    if (num == null) {
        console.log(a.match(/[a-z] /)   "1")
    } else {
        let s = num[0];
        let snum = ""   (Number(s)   1);
        while (snum.length < s.length)
            snum = "0"   snum;
        console.log(a.match(/[a-z] /)   snum);
    }
}

sumStrings("hello002")

Output: hello003

CodePudding user response:

You have to split the letters and the numbers...

About the numbers part... To know how many leading zeros there is, you can compare its length to the "parsed-and-restringified" length.

Then add 1 and redo the check. Maybe you have an extra leading zero now.

Notice the handy repeat() method in the return statement ;)

sumStrings = (a) => {
  
    // Get the letters part
    let letters = a.match(/[a-z] /g)[0];
    let lettersLength = letters.length;

    // Get the numbers part (the remainder)
    let numbers = a.substring(lettersLength);

    // Now parse the numbers as an integer
    let numbers_int = parseInt(numbers);
    
    // Then count how namy leading zeros are in front
    let numbersLength_str = numbers.length
    let numbersLength_int = numbers_int.toString().length
    let leadingZeros = numbersLength_str - numbersLength_int
    
    // Calculate the new number
    let newNumber = numbers_int   1;
  
    // If that new number is not the same length, decrease the leadingZeros count
    if(leadingZeros > 0 && newNumber.toString().length > numbersLength_int){
      leadingZeros--;
    }
      
    return letters   "0".repeat(leadingZeros)   newNumber

  }
  
  console.log(sumStrings("hello002")) // Expecting "hello003"
  
  console.log(sumStrings("hello009")) // Expecting "hello010"
  
  console.log(sumStrings("hello999")) // Expecting "hello1000"
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related