简体   繁体   English

检查两个字符串是否是排列 error.equals in not a function

[英]Check if two strings are permutations error .equals in not a function

Currently trying to check if two strings are permutations of one another by sorting both and checking if they are equal - permutations have the same characters, just in different orders.目前试图通过对两个字符串进行排序并检查它们是否相等来检查两个字符串是否是彼此的排列 - 排列具有相同的字符,只是顺序不同。 In my example "dog" and "god" are permutations of one another.在我的例子中,“狗”和“神”是彼此的排列。

I have implemented the below code which I believe is correct.我已经实现了以下我认为是正确的代码。 I made changes through looking at why this might be the case online, but I'm still stuck on why i'm getting this error as.equals() seems to be used widely to compare two strings in JavaScript.我通过查看在线可能出现这种情况的原因进行了更改,但我仍然坚持为什么我会收到此错误,因为.equals() 似乎被广泛用于比较 JavaScript 中的两个字符串。

The error is:错误是:

if (sort(s1).equals(sort(s2))) { if (sort(s1).equals(sort(s2))) {

TypeError: sort(...).equals is not a function TypeError: sort(...).equals 不是 function

 function sort(string) { return string.split("").sort().join(""); } function isPermutation(s1, s2) { if (sort(s1).equals(sort(s2))) { return true; } return false; } console.log(isPermutation("dog", "god"));

Because strings in JavaScript don't have an equals method.因为 JavaScript 中的字符串没有equals方法。 You compare them via === .您可以通过===比较它们。

Also, you can simplify your isPermutation function by returning the result of the comparison directly.此外,您可以通过直接返回比较结果来简化您的isPermutation function。

 function sort(string) { return string.split("").sort().join(""); } function isPermutation(s1, s2) { return sort(s1) === sort(s2); } console.log(isPermutation("dog", "god"));

You should use === to compare strings:您应该使用===来比较字符串:

 function sort(string) { return string.split("").sort().join(""); } function isPermutation(s1, s2) { if (sort(s1) === (sort(s2))) { return true; } return false; } console.log(isPermutation("dog", "god"));

string equality in Javascript is achieved with === It might be worth looking up at the different ways Javascript handles equality as there are a few different concepts like abstract and strict equality Javascript 中的string相等是通过===实现的

For example例如

"0" == 0 // true
"0" == "0" //true
"0" === 0 // false
"0" === "0" //true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

As equals don't exist in javascript. javascript 中不存在等值。

You can register your own equals method你可以注册自己的equals方法

String.prototype.equals = function(that) {
    return this == that;
}

You can check if a value equals another by using either a "strict equality comparison" (===) or a "abstract equality comparison" (==).您可以使用“严格相等比较”(===)或“抽象相等比较”(==)来检查一个值是否等于另一个值。

So in your case you either get: if (sort(s1) === sort(s2)) .因此,在您的情况下,您会得到: if (sort(s1) === sort(s2)) or: if (sort(s1) == sort(s2)) .或: if (sort(s1) == sort(s2))

The main difference here is that the "abstract equality comparison" (==) will convert types before comparing.这里的主要区别是“抽象相等比较”(==)将在比较之前转换类型。 So for example 0 == "0" will return true .因此,例如0 == "0"将返回true but 0 === "0" will return false .0 === "0"将返回false

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

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