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

标签 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;
}

最佳答案

使用这个更正后的代码片段

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

我已经更正了它们,您可以看到它们都返回 3。

我已经更正了这些行

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

关于javascript - 为什么这个 JavaScript 函数对同一查询返回两个不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47235491/

相关文章:

function - 使用 MSI 的 Azure 函数 - 请求 token 时出错

javascript - 移相器 : Calling update function not working

javascript - 长度超过 16 个字符的字符串的 parseFloat

javascript - 如何仅使用 Javascript 裁剪图像?

javascript - 如何延迟淡入并从 onclick 滑动 div

c - 将消息存储在二维数组中的函数(在 C 编程中)

javascript - Ajax 数据管理

javascript - 即使它是直接复制,Bootstrap 模式也不会出现

python - 如何将打印输出分配给变量?

C++使一个类函数成为另一个类函数的成员