Results 1 to 7 of 7

Thread: Adding 1 in to each of array with Separators in javascript

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2021
    Posts
    16

    Question Adding 1 in to each of array with Separators in javascript

    Adding 1 to each in array from 1 to 15 maximum.
    with Numeric Separators .

    Code:
    var n = "11_11_11_11_11_11_11_11_11_11";
    var addone = "1";
    var maxNum = "15";
    var separator = "_";
    
    function addNum(n, separator, addone, maxNum) {
         return result.join("_");
    }

    Result
    Code:
    ... "11_11_11_11_11_11_11_11_11_12"
    ... "11_11_11_11_11_11_11_11_11_13"
    ... "11_11_11_11_11_11_11_11_11_14"
    ... "11_11_11_11_11_11_11_11_11_15"
    ... "11_11_11_11_11_11_11_11_12_1"
    ... "11_11_11_11_11_11_11_11_12_2"
    ... "11_11_11_11_11_11_11_11_12_3"
    Is it possible to add a large numbers with Separators ?
    Last edited by dday9; Jun 23rd, 2023 at 03:43 PM.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Adding 1 in to each of array with Separators in javascript

    Split function puts the string into an array.

    Join turns the array back into a string.

    The rest of the logic seems quite simple - and maybe even recursive. Do you need to see finished code?

    What have you got so far?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2021
    Posts
    16

    Re: Adding 1 in to each of array with Separators in javascript

    Hi

    Code:
    var n = "11_11_11_11_11_11_11_11_11_11";
    var addone = "1";
    var maxNum = "15";
    var separator = "_";
    
    function addNum(n, separator, maxNum) {
    n = n.split(separator).reverse();   
        let maxLen = n.length;
        let sum = [];
        let remainder = 0;
    
        for (let i = 0; i < maxLen; i++) {
          let x = parseInt(a[i]) ? parseInt(a[i]) : 0;
          let digit = (x + remainder);
          remainder = Math.floor((x + remainder));
          sum.unshift(digit);
        }
        if (remainder) {sum.unshift(remainder)}
        
        return sum.join("_");
      //return sum.filter(n => { return n; } ).join('_');
    }
    I have tried this function but it's not count up ...
    Last edited by dday9; Jun 23rd, 2023 at 03:43 PM.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Adding 1 in to each of array with Separators in javascript

    @optikal - did you solve this yet?
    Last edited by szlamany; Jun 13th, 2023 at 02:22 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2021
    Posts
    16

    Re: Adding 1 in to each of array with Separators in javascript

    Hi szlamany ,thank you for your attention.

    I am working hard to solve it.
    But It's not working yet with this function too
    It's seems very very difficult...

    Code:
    function addNum(arr, separator, maxNum) {
        arr = arr.split(separator).reverse();
        let maxLen= Math.max(arr.length);
        let sum = [];
        let remainder = 0;
        for (let i = 0; i < maxLen; i++) {
          let x = parseInt(arr[i]) ? parseInt(arr[i]) : 0;
          let digit = (x + remainder) % 10;
          remainder = Math.floor((x + remainder) / 10);
          sum.unshift(digit);
        }
        if (remainder) {sum.unshift(remainder)}
        
        return sum.join("");
    	//return sum.filter(n => { return n; } ).join('-');
    }
    Last edited by dday9; Jun 23rd, 2023 at 03:43 PM.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Adding 1 in to each of array with Separators in javascript

    This works for me:

    Code:
    function addNum(n, separator, maxNum, addone) {
        n = n.split(separator).reverse();
        let maxLen = n.length;
        let sum = [];
        let max = parseInt(maxNum, 10);
        let remainder = parseInt(addone, 10);
    
        for (let i = 0; i < maxLen; i++) {
            let x = parseInt(n[i]) ? parseInt(n[i]) : 0;
            let digit = (x + remainder);
            if (digit > max) {
                digit = 1;
                remainder = 1;
            } else {
                remainder = 0;
            }
            sum.unshift(digit);
        }
    
        return sum.join("_");
    }
    Tested with this loop

    Code:
    function testNum() {
    
        var n = "11_11_11_11_11_11_11_11_11_11";
        var addone = "1";
        var maxNum = "15";
        var separator = "_";
    
        for (var i = 0; i < 100; i++) {
            n = addNum(n, separator, maxNum, addone);
            alert(n);
        }
    
    }
    Cool code to write - thanks for the exercise!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2021
    Posts
    16

    Re: Adding 1 in to each of array with Separators in javascript

    BOOOM Thats it szlamany.
    thank you for your time.
    Cool

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width