I have a sample array as
const arr=[0,2,4,6]
I want to check if they have a common difference in them, which is 2
and yes in above array.
There could be more efficient way to do this. I tried using a simple for
loop as:
const arr = [0,2,4,6];
for(let i=0;i<arr.length-1;i++){
if(arr[i]+2==arr[i+1]){
console.log(true);
}
else{
console.log(false);
}
}
This could give me result as true
,true
,true
and push all values to a new array then check if all are true and finally I would get true
It would be helpful if there is a more efficient way to do this; any help appreciated.