Equality of corresponding elements in JavaScript


We are required to write a JavaScript function that takes in two arrays of literals. The function should check the corresponding elements of the array. The function should return true if all the corresponding elements of the array are equal otherwise it should return false.

Example

The code for this will be −

const arr1 = [6, 7, 8, 9, 10, 11, 12, 14];
const arr2 = [6, 7, 8, 9, 10, 11, 12, 14];
const areEqual = (first, second) => {
   if(first.length !== second.length){
      return false;
   };
   for(let i = 0; i < first.length; i++){
      if(first[i] === second[i]){
         continue;
      }
      return false;
   };
   return true;
};
console.log(areEqual(arr1, arr2));

Output

The output in the console −

True

Updated on: 14-Oct-2020

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements