Are array of numbers equal - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in two arrays of Numbers, say first and second and checks for their equality.

The arrays will be considered equal if −

  • They contain the same elements and in the same order.
  • The product of all the elements of the first array and second array is equal.

The first array of numbers −


const first = [3, 5, 6, 7, 7];

The second array of numbers −

const second = [7, 5, 3, 7, 6];

Example

Following is the code −

const first = [3, 5, 6, 7, 7];
const second = [7, 5, 3, 7, 6];
const isEqual = (first, second) => {
   const prodFirst = first.reduce((acc, val) => acc*val);
   const prodSecond = second.reduce((acc, val) => acc*val);
   if(prodFirst === prodSecond){
      return true;
   };
   for(let i = 0; i < firstCopy.length; i++){
      if(first[i] === second[1]){
         continue;
      };
      return false;
   };
   return true;
};
console.log(isEqual(first, second));

Output

This will produce the following output in console −

true
raja
Published on 30-Sep-2020 13:51:19
Advertisements