为什么几次迭代后"undefined"?



下面的代码发生了一件奇怪的事情。该代码仅在一个例外情况下顺利运行 - 几次迭代后,我得到了"未定义"。

您可以通过运行几次来测试它。首先,您将获得常规输出(三个随机城市(,然后在某个时候您将获得"未定义"。在少于10次迭代中发生。

为什么这样做?我想念什么?

var coolWords = ["Amsterdam", "Berlin", "Sofia", "Prague", "Lisbon", "London", "Malaga", "Copenhagen", "Milan"];
    
var newList = [];
    
function niceTripleCombo(coolWords) {
  for (let i = 0; i < 3; i++) {
    var combo = coolWords[Math.floor(Math.random()*coolWords.length)];
    newList.push(" " + combo);
  };
};
    
function iterator(x) {
  if (newList[0] !== newList[1] && newList[1] !== newList[2] && newList[1] !== newList[3]) {
    return newList;
  } else {
    return niceTripleCombo(coolWords);
  }
};
    
    console.log(iterator(niceTripleCombo(coolWords)));

让我们考虑此测试运行。

initiatelly, iterator(niceTripleCombo(coolWords))来自 console.log()

现在将调用niceTripleCombo(coolWords)

Nicetipliplecombo的开始(coolwords(

现在进行循环,

i = 0,假设我们得到newList = [" Milan"]

i = 1,假设我们得到newList = [" Milan", " London"]

i = 2,假设我们得到newList = [" Milan", " London", " London"]

nicetipliplecombo的结束(coolwords(

最后,我们有米兰,伦敦,伦敦

现在在iterator()中,

由于我们有2个伦敦,if将失败,else将运行。

现在这是第一个错误,您没有初始化newList为空。因此,当niceTripleCombo再次运行时,它将增加三个城市。

,您可能会得到[" Milan", " London", " London", " Amsterdam", " Malaga", " Sofia"]

现在第二return niceTripleCombo(coolWords);不会返回任何值。

为什么它不会返回任何东西,因为,您正在返回一个没有返回语句的函数[ niceTripleCombo(coolWords)]。

您的功能必须返回任何值,或者如果返回 功能,该功能必须返回一个值。在您的情况下 您没有niceTripleCombo(coolWords)中的返回语句 默认情况下,JS函数返回undefined,如果没有其他值为 返回。

这就是为什么您要获得undefined

因此,将您的else在您的iterator中更改为:

else {
  newList = [];
  niceTripleCombo(coolWords);
  return iterator();
}

现在您不应该获得undefined。接下来,无需将niceTripleCombo传递给iterator。由于您没有在您内部使用niceTripleCombo函数iterator

在MDN中阅读有关return的更多信息:返回语句

当您的新清单数组具有以下值时,

 [" Berlin", " Berlin", " London"]

或类似数组元素相同的地方,如果迭代器中的条件失败,并且您正在执行

niceTripleCombo(coolWords)

没有返回值,因此返回未定义。

您的代码上有几个注释:

  • 您是在吗?
  • 如果结果是要获得独特的三个单词,那么如果Randomiser不给您正确的数字,此方法将浪费很多周期。
  • 如果您要传递一个数组,为什么要使它成为全局变量?这将使您的迭代器毫无意义。
  • 您猜测功能没有返回值 - 因此它返回undefined

我的建议是:

var coolWords = ["Amsterdam", "Berlin", "Sofia", "Prague", "Lisbon", "London", "Malaga", "Copenhagen", "Milan"];
function niceTripleCombo( list ) {
  // Make a copy of the list so we don't modify the original.
  let words = list.slice();
  let result = [];
  
  for( let i = 0; i < 3; i++ ){
    
    // Use splice to get a word out of your copy array
    // It also removes it from the array,
    // so next time we cannot guess this word anymore.
    result.push(
      words.splice( Math.floor( Math.random() * words.length ), 1 )[ 0 ]
    );
    
  }
  
  return result;
  
};
console.log( niceTripleCombo(coolWords) );

这将始终返回三个唯一单词的列表,并通过从数组中挑选一个随机单词,将其从数组中删除并将其添加到答案中,然后重复使用现在较短的数组。这样,只需运行此周期三次次,而不是可能的无限金额,则如果Randomiser不在您身边。

newList数组应在niceTripleCombo方法中返回。

var coolWords = ["Amsterdam", "Berlin", "Sofia", "Prague", "Lisbon", "London", "Malaga", "Copenhagen", "Milan"];
var newList = [];
function niceTripleCombo(coolWords) {
  newList.length = 0; // Empty the array before starting
  for (let i = 0; i < 3; i++) {
    var combo = coolWords[Math.floor(Math.random()*coolWords.length)];
    newList.push(" " + combo);
  };
  return newList; //Returning an array.
};
function iterator(x) {
  if (newList[0] !== newList[1] && newList[1] !== newList[2] && newList[1] !== newList[3]) {
    return newList;
  } else {
    return niceTripleCombo(coolWords);
  }
};
console.log(iterator(niceTripleCombo(coolWords)));

相关内容

最新更新