 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Checking for squared similarly of arrays in JavaScript
Problem
We are required to write a JavaScript function that takes in two arrays of numbers, arr1 and arr2, as the first and second argument respectively.
Our function should return true if and only if every element in arr2 is the square of any element of arr1 irrespective of their order of appearance.
For example, if the input to the function is −
Input
const arr1 = [4, 1, 8, 5, 9]; const arr2 = [81, 1, 25, 16, 64];
Output
const output = true;
Example
Following is the code −
const arr1 = [4, 1, 8, 5, 9]; const arr2 = [81, 1, 25, 16, 64]; const isSquared = (arr1 = [], arr2 = []) => {    for(let i = 0; i < arr2.length; i++){       const el = arr2[i];       const index = arr1.indexOf(el);       if(el === -1){          return false;       };    };    return true; }; console.log(isSquared(arr1, arr2)); Output
true
Advertisements
 