0

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?

2
  • 1
    Shouldn't sum of first number be 102? Commented May 19, 2016 at 20:25
  • 1
    just push your result into a new array. Commented May 19, 2016 at 20:26

7 Answers 7

3

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);

Sign up to request clarification or add additional context in comments.

Comments

3

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);

Comments

1

One more "you could just":

x.map(function (num) {
  return [num, eval(num.replace(/\D*(\d)\D*/g, "$1+") + "0")];
});

Comments

0

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);

Comments

0

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']);

Comments

0
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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.