简体   繁体   English

为什么我的脚本说 '(' === ')' 是真的?

[英]Why is my script saying '(' === ')' is true?

I was doing this kata on codewars.我在 codewars 上做这个 kata The question wants the function to return true if the first argument (string) passed in ends with the 2nd argument (also a string).如果传入的第一个参数(字符串)以第二个参数(也是一个字符串)结尾,则该问题希望函数返回 true。 So I wrote my function and everything worked just fine until it compares ':-)' with ':-(' and returns true.所以我写了我的函数,一切都很好,直到它比较':-)'':-('并返回true。

What is wrong?怎么了? I'm so confident that my code should work that I don't even know what to search for.我非常自信我的代码应该可以工作,我什至不知道要搜索什么。

 function solution(str, ending){ if (!ending) return true; // if ending is a empty string return true (the question wants that) let ok; const strArr = str.split(''), endingArr = ending.split(''); for (let i = 0; i < endingArr.length; i++) strArr.reverse()[i] === endingArr.reverse()[i] ? ok = true : ok = false; return ok; } console.log(solution(":-)",":-("));

Your problem is a misunderstanding of what reverse() does.您的问题是对reverse()作用的误解。 It does not return a reversed copy of the old array, it reverses the existing array and returns that same array.它不返回旧数组的反转副本,而是反转现有数组并返回相同的数组。 As a result, you keep reversing the arrays back and forth every iteration of the loop, causing some elements to be skipped and some to be checked twice.结果,您在循环的每次迭代中不断来回反转数组,导致某些元素被跳过,某些元素被检查两次。

Array.prototype.reverse() on MDN MDN 上的 Array.prototype.reverse()

Edit:编辑:

As pointed out by others in the comments, both to the question and this answer, there are in fact multiple problems.正如其他人在评论中指出的那样,对于问题和这个答案,实际上存在多个问题。

reverse() aside, the loop always sets ok to the result of the last comparison, making the function ignore all previous results.撇开reverse()不谈,循环总是将上次比较的结果设为ok ,从而使函数忽略所有先前的结果。

The easier way to implement this is to remove ok altogether.实现这一点的更简单方法是完全删除ok Instead, return false as soon as a mismatch is detected.相反,一旦检测到不匹配,就返回false If the function runs long enough to exit the loop, it means no mismatch was detected and true can be returned.如果函数运行的时间足够长以退出循环,则意味着没有检测到不匹配并且可以返回true

Edit 2:编辑2:

Just as a friendly suggestion:就像一个友好的建议:

While both reverse() and ok are real issues with the code, I only noticed the first one the first time around due to the formatting of the code.虽然reverse()ok都是代码的真正问题,但由于代码的格式,我第一次只注意到第一个。 The ok problem was off-screen due to the line being too long.由于线路太长, ok问题是屏幕外。 As such, once I spotted the reverse() issue, I assumed that was it and didn't bother scrolling sideways to see the rest of the code.因此,一旦我发现了reverse()问题,我就认为就是这样,并没有费心横向滚动以查看其余代码。

I am not going to demand that you write your own code in a certain way, but if you format it properly, it allows others to read it more easily.我不会要求您以某种方式编写自己的代码,但是如果您正确格式化它,它会让其他人更容易阅读。 In essence, you help us to more easily help you.从本质上讲,您帮助我们更轻松地帮助您。

For instance, this line:例如,这一行:

for (let i = 0; i < endingArr.length; i++) strArr.reverse()[i] === endingArr.reverse()[i] ? ok = true : ok = false;

...would have been significantly easier to read as... ……阅读起来会容易得多……

for (let i = 0; i < endingArr.length; i++) {
  if(strArr.reverse()[i] === endingArr.reverse()[i])
    ok = true;
  else
    ok = false;
}

...or some variation thereof. ...或其一些变体。 Here, the problem is significantly more visible and obvious.在这里,问题明显更加明显。

The other answer explains many of the mistakes you've made.另一个答案解释了您所犯的许多错误。 I wanted to point out just how much you've over-thought your solution.我想指出您对解决方案的过度思考。

 function solution(str, ending){ if (ending === "") return true; // if ending is a empty string return true (the question wants that) return str.endsWith(ending); } console.log(solution(":-)",":-(")); console.log(solution("foo","")); console.log(solution("foo","bar")); console.log(solution("foobar","bar"));

Even my solution above is overengineered, str.endsWith("") always returns true.即使我上面的解决方案是str.endsWith("")设计的, str.endsWith("")总是返回 true。 So this can be simplified further.所以这可以进一步简化。

 function solution(str, ending){ return str.endsWith(ending); } console.log(solution(":-)",":-(")); console.log(solution("foo","")); console.log(solution("foo","bar")); console.log(solution("foobar","bar"));

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

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