0

Given two array of same length, return an array containing the mathematical difference of each element between two arrays.

Example:

a = [3, 4, 7]

b = [3, 9, 10 ]

results: c = [(3-3), (9-4), (10,7)] so that c = [0, 5 3]

let difference = []

function calculateDifferenceArray(data_one, data_two){
    let i = 0
    for (i in data_duplicates) {
        difference.push(data_two[i]-data_one[i])
    }
    console.log(difference)
    return difference
}
calculateDifferenceArray((b, a))

It does work.

I am wondering if there is a more elegant way to achieve the same

Magofoco
  • 5,098
  • 6
  • 35
  • 77
  • 7
    https://codereview.stackexchange.com/ (Also, you could `map` the array and use the index parameter to calculate the difference) – adiga Nov 14 '19 at 12:44
  • 2
    How does this _"work"_ when the `for...in...` loop iterates over a totally unrelated object? o.O – Andreas Nov 14 '19 at 12:46
  • What @Andreas said. Not to mention that it only passes a single argument to `calculateDifferenceArrays` and has the subtraction backward (according to th example data and expected result. – T.J. Crowder Nov 14 '19 at 12:56

3 Answers3

3

for-in isn't a good tool for looping through arrays (more in my answer here).

"More elegant" is subjective, but it can be more concise and, to my eyes, clear if you use map:

function calculateDifferenceArray(data_one, data_two){
    return data_one.map((v1, index) => data_two[index] - v1)
}
calculateDifferenceArray(b, a) // < Note just one set of () here

Live Example:

const a = [3, 4, 7];

const b = [3, 9, 10 ];

function calculateDifferenceArray(data_one, data_two){
    return data_one.map((v1, index) => v1 - data_two[index]);
}
console.log(calculateDifferenceArray(b, a));

or if you prefer it slightly more verbose for debugging et. al.:

function calculateDifferenceArray(data_one, data_two){
    return data_one.map((v1, index) => {
        const v2 = data_two[index]
        return v1 - v2
    })
}
calculateDifferenceArray(b, a)

A couple of notes on the version of this in the question:

  • It seems to loop over something (data_duplicates?) unrelated to the two arrays passed into the method.

  • It pushes to an array declared outside the function. That means if you call the function twice, it'll push the second set of values into the array but leave the first set of values there. That declaration and initialization should be inside the function, not outside it.

  • You had two sets of () in the calculateDifferenceArray call. That meant you only passed one argument to the function, because the inner () wrapped an expression with the comma operator, which takes its second operand as its result.

  • You had the order of the subtraction operation backward.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

Use map as following:

const a = [3, 4, 7]
const b = [3, 9, 10]
const c = b.map((e, i) => e - a[i])
// [0, 5, 3]
ferhatelmas
  • 3,818
  • 1
  • 21
  • 25
-1

You could use higher order array method map. It would work something like this:

let a = [2,3,4];
let b = [3,5,7];
let difference = a.map((n,i)=>n-b[i]);
console.log(difference);

you can read more about map here