Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Are array of numbers equal - JavaScript
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
Advertisements