0

I have a question about how to compare values inside array.

In particular I read data from an external device and put these data inside an array using:

this.timeArray.push(this.externalDevice)

Now my problem is that I have an array like:

(372) this.timeArray: [Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1),.....]

EDIT: If I expand the array they are like this:

[0 … 99]
0: [20]
1: [40]
2: [60]
3: [80]
4: [100]
5: [120]
6: [140]
7: [160]
8: [180]

Where every values is a number, like 20, 40, 60, .... or -20, -40, -60, ....

Now I have to check that each value is 20 different from the next value.

So for example, if the difference from the second value and the first one is 20, it is correct.

I have tried to used a foreach, but I don't understand how to compare the values, because I have to compare the first with the second value, the second with the third etc.. How can I do?

1
  • So is this an array of arrays? like [[20],[40],[60],[80]]? or just a straight array like [20,40,60,80] Commented Aug 31, 2020 at 13:06

2 Answers 2

1

I would iterate using a normal for loop - something like this:

const validateArray = (myArray) => {
  for(let i=1; i<myArray.length; i++){
    if(Math.abs(Math.abs(myArray[i][0]) - Math.abs(myArray[i-1][0])) !== 20){
      return false;
    }
  }
  return true;
}

You can then pass the array you wish to validate, and this function will return true if the array is valid.

Edit:

By using Math.abs, we avoid checking if values are positive before performing the subtraction.

Edit:

If you wish to report errors, change this code to add the indices to another array as follows:

const validateArray = (myArray) => {
let errors = []
  for(let i=1; i<myArray.length; i++){
    if(Math.abs(Math.abs(myArray[i][0]) - Math.abs(myArray[i-1][0])) !== 20){
      errors.push([i-1,i])
    }
  }
  return errors;
}

This will return an array of indices at which there are errors in the form:

[[0,1]...[4,5]]

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

7 Comments

I have tried your method using directly the for, but It compares only the first and the second value
Yes, it will return if it finds the array is invalid, so if the difference between two values is not 20, no other values will be compared.
Here is a runnable example jsfiddle.net/1qk4v7u6
mmh so if it finds an error it stops. What if I wanted him to check all the values ​​and report any errors?
Instead of returning, add the values to a new array and return the array at the end
|
0

I'd do it by 1) flattening the array of arrays and then 2) comparing the values inside .every() to get the boolean whether your condition matches:

const checkSteps = (arrToCheck = [], step = 0) => arrToCheck.every((val, i, ref) => (i === 0 || val - step === ref[i-1]));

const origArray = [[20],[40],[60]];
const flattenedArray = origArray.flat();
const hasDifferenceOfTwenty = checkSteps(flattenedArray, 20);
const hasDifferenceOfFifteen = checkSteps(flattenedArray, 15);

console.log('hasDifferenceOfTwenty', hasDifferenceOfTwenty);
console.log('hasDifferenceOfFifteen', hasDifferenceOfFifteen);

Since the first value is "initial value", hence we have to exclude it in the function (i === 0);

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.