为什么这个javascript函数会为同一个查询返回两个不同的结果?

时间:2017-11-11 07:23:54

标签: javascript function

我想做的事情:
我试图计算字符串中两个单词之间的距离(在我们的示例中为var someString = "one two three four five six seven"。正如您在下面的代码中所看到的,我将字符串转换为带有split()的数组方法,然后调用计算单词之间距离的函数minDist()

问题是什么:
正如您在代码和我的相关注释中所看到的,如果我直接使用字符串(或者更确切地说,数组"one", "two", "three", "four", "five", "six", "seven")调用函数,那么一切都按预期工作。结果是,正如预期的那样,3 - 这是"一个"之间的距离。和"四"在这个具体的例子中 但是,正如您所看到的,如果我尝试使用数组的变量调用该函数,则结果为null。
为什么会这样,我该如何解决这个问题?



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;
}
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

因为[list2b]不是你所期望的。

这是一个包含一个字符串的数组:

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

正如您所看到的,它还有额外的引号

答案 1 :(得分:1)

使用此更正后的代码段

在一个你发送一个值的数组中,你发送了一个数组值。第二个错误是在值之后追加“”;字符串值已在js中的数组中引用,您无需添加引号。

我已经纠正了他们两个,你可以看到两人都回来了。

我已经纠正了这些行

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

你可以省略这个循环

//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;
}

答案 2 :(得分:1)

首先,您不需要添加&#34;&#34;如果您假装将其用作数组,则为字符串。您的函数需要一个字符串数组,而不是一个字符串。 在片段中,我向您展示了一个工作版本。 因为你正在为数组使用一个字符串,所以要做的就是让它回到一个带有split的真实数组(&#34;,&#34;)(注意我摆脱了.join(&#34) ;,&#34;))

&#13;
&#13;
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;
}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

这是因为list2b是一个字符串,[list2b]是只有一个值的数组。如果删除for循环并用distanceCheck替换[list2b],它应该可以工作。