how to see if 2 numbers in an array are equal in javascript code example
Example 1: check if 2 arrays are equal javascript
const a = [1, 2, 3];
const b = [4, 5, 6];
const c = [1, 2, 3];
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
arrayEquals(a, b); // false
arrayEquals(a, c); // true
Example 2: check if 2 arrays are equal javascript
var arraysMatch = function (arr1, arr2) {
// Check if the arrays are the same length
if (arr1.length !== arr2.length) return false;
// Check if all items exist and are in the same order
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) return false;
}
// Otherwise, return true
return true;
};
Example 3: javascript check if an array has same consecutive values
const arr = ["g", "z", "z", "v" ,"b", "b", "b"];
const checkThree = arr => {
const prev = {
element: null,
count: 0
};
for(let i = 0; i < arr.length; i++){
const { count, element } = prev;
if(count === 2 && element === arr[i]){
return true;
};
prev.count = element === arr[i] ? count + 1 : count;
prev.element = arr[i];
};
return false;
};
console.log(checkThree(arr));
console.log(checkThree(["z", "g", "z", "z"]));