0

I have two arrays of values which represent the Y value. I am trying to determine if line1 and line2 crossover/under each other during the set and if possible, return the index in the array they do. I have found code that can tell me a specific line but trying to see if there a better way to do this?

The data sets are below. line1[0] would be the most recent number in the series. Below I would like to test if line2 crosses over line1 and this would return index 1.

line1 = [25, 30, 25, 20, 15, 10]
line2 = [ 60, 40, 20, 17, 11, 5]
boxxa
  • 37
  • 6

1 Answers1

0
var line1 = [25, 30, 25, 20, 15, 10];
var line2 = [ 60, 40, 20, 17, 11, 5];
var start=line1[0]<line2[0];
var crossingpoint=line1.findIndex((el,i)=>start!==(el<line2[i]));

Simply check if linepoint0 is bigger linepoint1 changes. In your case it will return 2, cause it crosses between 1 and 2. You may substract 1 to get the desired output...

try it

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • This is correct if you do not consider equal values (i.e. positions where the arrays "touch") to be "crossing", which may or may not be the desired result. If you _do_ want to consider equal values to be "crossing" then add an equality test, i.e. add `el===line2[i]||` just before `start` in the final line. – Andrew Willems Jun 23 '17 at 23:27