
Jun 13th, 2023, 12:35 AM
#1
Thread Starter
Junior Member
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.

Jun 13th, 2023, 05:08 AM
#2
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?

Jun 13th, 2023, 06:44 AM
#3
Thread Starter
Junior Member
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.

Jun 13th, 2023, 02:16 PM
#4
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.

Jun 14th, 2023, 12:41 AM
#5
Thread Starter
Junior Member
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.

Jun 14th, 2023, 07:11 AM
#6
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!

Jun 14th, 2023, 09:40 AM
#7
Thread Starter
Junior Member
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

Forum Rules

Click Here to Expand Forum to Full Width
