0
var a = [1, 5, 7, 3];

console.log(DiffTheArray(a));

With the expected result of [4, 2, -4], which is the difference between every two elements of the original array, how would DiffTheArray most concisely be implemented?

I was hoping to implement it using slices (a.slice(1) - a.slice(0,-1)), but there doesn't seem to be a built-in way of subtracting one array from another in JS.

Blackhawk
  • 5,984
  • 4
  • 27
  • 56

4 Answers4

2

You can use Array.prototype.reduceRight()

var a = [1, 5, 7, 3];
var res = [];
a.reduceRight(function(a, b, index) {
  res[index] = a - b;
  return b
});

console.log(res);
guest271314
  • 1
  • 15
  • 104
  • 177
  • Wait, so you're abusing `reduceRight()` to iterate through the array by storing each successive element in the accumulator? COOL! – Blackhawk Oct 18 '16 at 03:54
  • Requirement could also probably be achieved with an array set as accumulator where additional logic is included to handle first index of resulting array not being set as `NaN` – guest271314 Oct 18 '16 at 04:08
  • related link using `map` to iterate through two arrays at the same time: http://stackoverflow.com/a/22015771/2832561 – Blackhawk Oct 18 '16 at 04:16
  • There are several different approaches which could be used to achieve expected result. – guest271314 Oct 18 '16 at 04:21
1

You could just iterate the entire array:

function DiffTheArray(arr) {
  var diffArr = [];

  for (var i = 0; i < arr.length - 1; i++) {
    diffArr.push(arr[i + 1] - arr[i]);
  }

  return diffArr;
}

console.log(DiffTheArray([1, 5, 7, 3]));
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
  • I appreciate the explicit method, and I'm sure it's probably pretty fast, but in this case my character count is limited (see [hackmud](http://store.steampowered.com/app/469920/)), so I'm looking for a more concise functional way. – Blackhawk Oct 18 '16 at 03:49
1

You can also just use the regular reduce function:

function diffTheArray(arr) {
  let diff = [];
  arr.reduce((a, b) => {
    diff.push(b - a);
    return b;
  });
  return diff;
}

var a = [1, 5, 7, 3];
console.log(diffTheArray(a));

If you use the comma operator, you can get the return onto the same line and take advantage of the lighter syntax of es6:

function diffTheArray(arr) {
  let diff = [];
  arr.reduce((a, b) => (diff.push(b - a), b));
  return diff;
}

console.log(diffTheArray([1, 5, 7, 3]));
KevBot
  • 17,900
  • 5
  • 50
  • 68
  • I'll check tonight to see if the => operator is supported in [hackmud](http://store.steampowered.com/app/469920/). Your solution is much the same as @guest271314, though I appreciate learning about some tasty syntactic sugar :) – Blackhawk Oct 18 '16 at 19:19
0

Based on how numpy solves this problem (arr[1:] - arr[:-1]) and this example of using map with two arrays, I was considering the following:

function diffTheArray(arr) {
    return arr.slice(1).map(function(e, i){return e-arr.slice(0,-1)[i];});
}

var a = [1,5,7,3];
console.log(diffTheArray(a));

However, this is 90 characters, and I think some of the other answers here are shorter, which is the most important metric for my situation.

Community
  • 1
  • 1
Blackhawk
  • 5,984
  • 4
  • 27
  • 56