简体   繁体   English

为什么此javascript函数针对同一查询返回两个不同的结果?

[英]Why does this javascript function return two different results for the same query?

What I'm trying to do: 我正在尝试做的是:
I'm trying to calculate the distance between two words in a string (in our example, var someString = "one two three four five six seven" . As you can see in my code below, I convert the string to an array with the split() method, then call the function minDist() that calculates the distance between the words. 我正在尝试计算字符串中两个单词之间的距离(在我们的示例中, var someString = "one two three four five six seven" 。如下面的代码所示,我将字符串转换为带有split()方法,然后调用函数minDist()来计算单词之间的距离。

What's the problem: 有什么问题:
As you can see in the code and my relevant comments, everything works as intended if I call the function using the string (or rather, the array, "one", "two", "three", "four", "five", "six", "seven" ) directly. 如您在代码和我的相关注释中所看到的,如果我使用字符串(或更确切地说,数组"one", "two", "three", "four", "five", "six", "seven"调用函数,则一切都会按预期工作"one", "two", "three", "four", "five", "six", "seven" )。 The result is, as expected, 3 - which is the distance between "one" and "four" in this specific example. 如预期的那样,结果为3-在此特定示例中为“一”和“四”之间的距离。
However, as you can see, if I try to call the function using the array's variable, the result is null. 但是,如您所见,如果我尝试使用数组的变量来调用函数,则结果为null。
Why is that and how can I resolve this problem? 为什么会这样,我该如何解决呢?

 var someString = "one two three four five six seven" var distanceCheck = someString.split(" "); console.log("distanceCheck",distanceCheck,"Quick test. As expected, the result is: one,two,three,four,five,six,seven") for (var i = 0; i < distanceCheck.length; i++) { distanceCheck[i] = '"' + distanceCheck[i] + '"'; } var list2b = distanceCheck.join(", "); console.log("list2b",list2b,'As expected, the result is: "one", "two", "three", "four", "five", "six", "seven"') var check0 = minDist(["one", "two", "three", "four", "five", "six", "seven"], "one", "four"); console.log("check0",check0,"Result is: 3") var check1 = minDist([list2b], "one", "four"); console.log("check1",check1,"Result is: null. WHY?") function minDist(words, wordA, wordB) { var wordAIndex = null; var wordBIndex = null; var minDinstance = null; for (var i = 0, length = words.length; i < length; i++) { if (words[i] === wordA) { wordAIndex = i; } if (words[i] === wordB) { wordBIndex = i; } if (wordAIndex !== null && wordBIndex !== null) { var distance = Math.abs(wordAIndex - wordBIndex); if (minDinstance === null || minDinstance > distance) { minDinstance = distance; } } } return minDinstance; } 

Because [list2b] is not what you expect. 因为[list2b]不是您所期望的。

It is an array with one string: 它是一个带有一个字符串的数组:

"\\"one\\", \\"two\\", \\"three\\", \\"four\\", \\"five\\", \\"six\\", \\"seven\\""

As you can see it additionally has extra quotes 如您所见,它还有额外的引号

Use this corrected snippet 使用此更正的代码段

In one you were sending an array of value in other you were sending a single value of array. 在一个中,您要发送一个值数组;在另一个中,您要发送一个数组值。 2nd mistake was appending "" after and before in the values; 第二个错误是在值的后面和前面加上“”; string values are already quoted in the array in js you need not add quotes. 字符串值已在js数组中加引号,您无需添加引号。

I have corrected both of them and you can see both are returning 3. 我已经纠正了这两个问题,您可以看到它们都返回了3。

I have corrected these lines 我已经更正了这些行

var check1 = minDist(distanceCheck, "one", "four");

and you can omit this loop 你可以省略这个循环

//for (var i = 0; i < distanceCheck.length; i++) {
  //distanceCheck[i] = '"' + distanceCheck[i] + '"';
//}

 var someString = "one two three four five six seven" var distanceCheck = someString.split(" "); console.log(distanceCheck, "Quick test. As expected, the result is: one,two,three,four,five,six,seven") //for (var i = 0; i < distanceCheck.length; i++) { //distanceCheck[i] = '"' + distanceCheck[i] + '"'; //} var list2b = distanceCheck.join(", "); console.log("list2b", list2b, 'As expected, the result is: "one", "two", "three", "four", "five", "six", "seven"') var check0 = minDist(["one", "two", "three", "four", "five", "six", "seven"], "one", "four"); console.log("check0", check0, "Result is: 3") var check1 = minDist(distanceCheck, "one", "four"); console.log("check1", check1, "Result is no longer null.") function minDist(words, wordA, wordB) { var wordAIndex = null; var wordBIndex = null; var minDinstance = null; for (var i = 0, length = words.length; i < length; i++) { if (words[i] === wordA) { wordAIndex = i; } if (words[i] === wordB) { wordBIndex = i; } if (wordAIndex !== null && wordBIndex !== null) { var distance = Math.abs(wordAIndex - wordBIndex); if (minDinstance === null || minDinstance > distance) { minDinstance = distance; } } } return minDinstance; } 

First, you do not need to add "" to a string if you pretend to use it as an array. 首先,如果您假装将其用作数组,则无需在字符串中添加“”。 Your function expects an array of strings, not a single string. 您的函数需要一个字符串数组,而不是单个字符串。 In the snippet i show you a working version. 在摘要中,我向您展示了一个有效的版本。 Because you are using a string for the array, the way to go is to get it back to a real array with split(",") (Note that i got rid of the .join(",")). 因为您在数组中使用字符串,所以方法是使用split(“,”)将其返回到实际数组中(请注意,我摆脱了.join(“,”))。

 var someString = "one two three four five six seven" var distanceCheck = someString.split(" "); console.log("distanceCheck",distanceCheck,"Quick test. As expected, the result is: one,two,three,four,five,six,seven") for (var i = 0; i < distanceCheck.length; i++) { distanceCheck[i] = '' + distanceCheck[i] + ''; } var list2b = distanceCheck.join(); console.log("list2b",list2b,'As expected, the result is: "one", "two", "three", "four", "five", "six", "seven"') var check0 = minDist(["one", "two", "three", "four", "five", "six", "seven"], "one", "four"); console.log("check0",check0,"Result is: 3") var check1 = minDist(list2b.split(","), "one", "four"); console.log("check1",check1,"Result is: null. WHY?") function minDist(words, wordA, wordB) { var wordAIndex = null; var wordBIndex = null; var minDinstance = null; for (var i = 0, length = words.length; i < length; i++) { if (words[i] === wordA) { wordAIndex = i; } if (words[i] === wordB) { wordBIndex = i; } if (wordAIndex !== null && wordBIndex !== null) { var distance = Math.abs(wordAIndex - wordBIndex); if (minDinstance === null || minDinstance > distance) { minDinstance = distance; } } } return minDinstance; } 

This is because list2b is one string, and [list2b] is an array with only one value. 这是因为list2b是一个字符串,而[list2b]是一个只有一个值的数组。 If you remove the for loop and replace [list2b] with distanceCheck it should work. 如果您删除了for循环并将[list2b]替换为distanceCheck,它应该可以工作。

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

相关问题 为什么我的JavaScript地理编码功能返回空白结果? - Why does my javascript geocode function return blank results? 为什么对同一函数的两个绑定返回不同的值 - Why two bindings to the same function return different values 为什么这个数学函数在Java和JavaScript中返回不同的值? - Why does this math function return different values in Java and JavaScript? 为什么此RegExp查询返回所有结果? - Why does this RegExp query return all results? 为什么这段代码有两个不同的结果? - Why does this code have two different results? 为什么这段JavaScript会给出不同的结果? - Why does this piece of javascript gives different results? 为什么我从相同数学表达式 [javascript] 的两个 forms 得到不同的结果? - Why am I getting different results from two forms of the same math expression [javascript]? JavaScript 函数在两个不同的时间间隔给出两种不同的结果 - JavaScript Function gives two different results at two different intervals of time 为什么HTML5 checkValidity()与JavaScript regexp.test()返回不同的结果? - Why does HTML5 checkValidity() return different results from JavaScript regexp.test()? 为什么这些Javascript函数产生不同的结果? - Why are these Javascript function producing different results?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM