Reputation: 5
I have a single dimension array of a series of numbers:
var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
I'm trying to insert the sum of the digits of each number alongside it to create a two-dimensional array, so that the output is:
[ [ '9493-4937-288383-8473', 96 ],
[ '4838-38403-8484', 65 ],
[ '9384-3848-4978-4944', 96 ],
[ '3920-2108-2845-1904', 58 ] ]
Unfortunately, my code:
for (var i = 0; i < x.length; i ++) {
var y = x[i].replace(/[- )(]/g,'');
var sum = 0;
var z = y;
while (z > 0) {
sum = sum + z % 10;
z = Math.floor(z / 10);
}
var xWithSum = [x[i]];
xWithSum.push(sum);
console.log(xWithSum);
}
results in the following output instead:
[ '9493-4937-288383-8473', 96 ]
[ '4838-38403-8484', 65 ]
[ '9384-3848-4978-4944', 96 ]
[ '3920-2108-2845-1904', 58 ]
That is, I'm ending up with four separate two-dimensional arrays rather than one two-dimensional array with four items.
Will someone please show me the error of my (newbie) JavaScript ways?
Upvotes: 0
Views: 1376
Reputation:
One more "you could just":
x.map(function (num) {
return [num, eval(num.replace(/\D*(\d)\D*/g, "$1+") + "0")];
});
Upvotes: 1
Reputation: 16660
You can use Array.prototype.reduce
to reduce your array of strings into the required format as below:
var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
var result = x.reduce(function(prev, current) {
var curr = current.replace(/-/g, "");
var sum = curr.split("").reduce(function (b, a) {
return parseInt(a, 10) + b;
}, 0);
prev.push([current, sum]);
return prev;
}, []);
console.log(result);
Upvotes: 0
Reputation: 386680
You could just iterate over the elements and over the characters for summing.
var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'],
y = x.map(function(a) {
return [a, a.split('').reduce(function(a, b) {
return a + (Number(b) || 0);
}, 0)];
});
console.log(y);
Upvotes: 3
Reputation: 3309
var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
// Loop through everything
for (var i = x.length - 1; i >= 0; i--) {
// Replace each element with [element, digitsSum]
x[i] = [x[i], getDigitsSum(x[i])];
}
// x now is as described
Upvotes: 0
Reputation: 16456
Couldn't you just... create an array above it and push the result to that array?
function converArr (x) {
resultArr = [];
for (var i = 0; i < x.length; i ++) {
var y = x[i].replace(/[- )(]/g,'');
var sum = 0;
var z = y;
while (z > 0) {
sum = sum + z % 10;
z = Math.floor(z / 10);
}
var xWithSum = [x[i]];
xWithSum.push(sum);
resultArr.push(xWithSum);
}
return resultArr;
}
convertArr(['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904']);
Upvotes: 0
Reputation: 781716
You need to push xWithSum
onto a result array.
var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
var result = [];
for (var i = 0; i < x.length; i++) {
var y = x[i].replace(/[- )(]/g, '');
var sum = 0;
var z = y;
while (z > 0) {
sum = sum + z % 10;
z = Math.floor(z / 10);
}
var xWithSum = [x[i], sum];
result.push(xWithSum);
}
console.log(result);
You could also use .map()
to run a function on each element of an array and return an array of the results.
var x = ['9493-4937-288383-8473', '4838-38403-8484', '9384-3848-4978-4944', '3920-2108-2845-1904'];
var result = x.map(function(el) {
var y = el.replace(/[- )(]/g, '');
var sum = 0;
var z = y;
while (z > 0) {
sum = sum + z % 10;
z = Math.floor(z / 10);
}
var xWithSum = [el, sum];
return xWithSum;
});
console.log(result);
Upvotes: 3
Reputation: 29926
Instead of
for (...) {
...
console.log(x);
}
do something like
var result = [];
for (...) {
...
result.push(x);
}
console.log(result);
Also your way of counting the sum will not work, as you are trying to load a 16 digit integer into a javascript number. That will not work, javascript numbers doesn't have that much precision. You can calculate the sum instead this way:
var sum = 0;
str.replace(/\d/g, x => sum+= +x);
console.log(sum);
Upvotes: 0