简体   繁体   English

CodeWars Javascript 挑战:它们是否“相同”?

[英]CodeWars Javascript Challenge: Are they the “same”?

The Kata: link卡塔: 链接

My solution:我的解决方案:

function comp(array1, array2) {
    let result;
    if (Array.isArray(array1) && Array.isArray(array2) && array1.length && array2.length) {
        result = true;
        const squares = array2.map(e => Math.sqrt(e));
        squares.forEach((e) => {
            if (array1.includes(e)) return;
            result = false;
        });
    } else {
        result = false
    }
    return result;
}

I don't want another solution.我不想要另一个解决方案。 I want to figure out why mine doesn't pass in all of the tests.我想弄清楚为什么我的没有通过所有的测试。 (Fails on two tests but I can't see which) (两次测试失败,但我看不到哪个)

I suspect the test expects true if both arrays are [] .我怀疑如果 arrays 都是[] ,则测试期望为true But the Kata's description says otherwise:但 Kata 的描述另有说明:

If a or b are nil (or null or None), the problem doesn't make sense so return false.如果 a 或 b 为 nil(或 null 或 None),则问题没有意义,因此返回 false。

Help would be appreciated.帮助将不胜感激。


Working Solution Based off of the answers:工作解决方案基于答案:

function comp(array1, array2) {
    let result;
    if (Array.isArray(array1) && Array.isArray(array2)) {
        result = true;
        const sortedArray1 = array1.sort((a, b) => a - b);
        const squares = array2.map(e => Math.sqrt(e)).sort((a, b) => a - b);
        squares.forEach((e, i) => {
            if (sortedArray1[i] === e) return;
            result = false;
        });
    } else {
        result = false
    }
    return result;
}

Okey, some cheating workarounds but your code fails here:好的,一些作弊解决方法,但您的代码在这里失败: 在此处输入图像描述 That's why after removing two .length you pass one more test.这就是为什么在删除两个.length之后你又通过了一个测试。

Next one is下一个是在此处输入图像描述 After .sqrt array2 you get 2, 3, 3 that's why you return true, but it's false..sqrt array2 之后你得到2, 3, 3这就是你返回 true 的原因,但它是 false。 That answer will be enough to help you solve this kata.这个答案足以帮助你解决这个 kata。

Btw.顺便提一句。

I suspect the test expects true if both arrays are [].如果 arrays 都是 [],我怀疑测试结果为真。 But the Kata's description says otherwise:但 Kata 的描述另有说明:

Kata's description:卡塔简介:

a or b might be [] (all languages except R, Shell). a 或 b 可能是 [](除 R、Shell 之外的所有语言)。 : - D : - D

PS I know for some people checking arguments maybe treated as cheating, I just do it for educational purposes. PS 我知道有些人检查 arguments 可能被视为作弊,我只是为了教育目的而这样做。 Black boxes are not always enough to figure out what's wrong.黑匣子并不总是足以找出问题所在。

if there is more then one number the same, but different count here and there, it will return true in your code, since it's includes in both arrays.如果有多个相同的数字,但这里和那里的计数不同,它将在您的代码中返回 true,因为它includes在 arrays 中。 you should sort it and iterate by index for compare, in order to make sure each item in the array used only once.您应该对其进行排序并按索引迭代以进行比较,以确保数组中的每个项目仅使用一次。

Here is what works for me:这对我有用:

function comp(array1, array2){
  if(!array1 || !array2) return false;
  array1 = array1.map(t => t**2).sort((a,b)=>a-b);
  array2 = array2.sort((a,b)=>a-b);

  for(let i=0;i<array1.length;i++){if(array1[i] !== array2[i])return false}
  return true;
}

sure youcan understand the idea and just make some twick in your existing code确保你能理解这个想法,并在你现有的代码中做一些调整

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM