Equality of corresponding elements in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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
raja
Published on 14-Oct-2020 11:49:08
Advertisements