在 JavaScript 中检查类似数组的平方
问题
我们需要编写一个JavaScript函数,该函数接受两个数字数组arr1和arr2,分别作为第一个和第二个参数。
当且仅当arr2中的每个元素都是arr1中任何元素的平方时,我们的函数应该返回true,而不管它们的出现顺序如何。
例如,如果函数的输入是-
输入
const arr1 = [4, 1, 8, 5, 9]; const arr2 = [81, 1, 25, 16, 64];
输出
const output = true;
示例
以下是代码-
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));输出结果true