Search code examples
javascriptarraysnode.jscomparison

how to compare sum of two values in array in NodeJs?


Basically I have two arrays. They are sorted. I have managed to compare them with efficiency. Now I require to find the sum of two values of arrays and compare them. For example

arr1 = [5,8,10] // sum of first two is 13
arr2 = [6,7,12] // sum of first two is also 13

if that happens, do something. I can't think of a way to do that. Any help will highly be appreciated.


Solution

  • If only value from first 2 index is what you required , then you can get the index and do the summation

    let arr1 = [5, 8, 10],
      arr2 = [6, 7, 12];
    
    if (arr1[0] + arr1[1] === arr2[0] + arr2[1]) {
      console.log(true)
    
    }