【问题标题】:If condition in a for loop to ensure a string is found in an arrayif 循环中的条件以确保在数组中找到字符串
【发布时间】:2021-12-30 15:37:44
【问题描述】:

使用从另一个函数随机生成的 3 个单词的数组,我试图获取一个函数来检查循环中的某个 i 单词(从图像文件的 src 给出)是否在从该其他函数生成的数组,如果不是 - 其他函数将再次运行,直到该单词包含在数组中。

var gFlavors = ['vanilla', 'chocolate', 'strawberry', 'lemon', 'peanutbutter']

function getFlavor(flavor) {
    var options = [];

    for (var i = 1; i <= gFlavors.length; i++) {
    // I have a folder of images, with the images named "1-vanilla.gif", "2-chocolate.gif" and so on. Using the loop, I am getting those images and setting a class and data for each
    flavor = new Image(150, 150);
    flavor.src = 'img/' + i + '-' + gFlavors[i - 1] + '.gif';
    flavor.setAttribute('class', 'flavor');
    flavor.setAttribute('data-flavor', gFlavors[i - 1]);
    var elFlavorsDiv.appendChild(flavor);

    // Slicing solely the flavor word found in each image filename
    var flavorImgSrc = flavor.attributes.src.nodeValue;
    var flavorImgSrcDash = flavorImgSrc.indexOf('-');
    var flavorStr = flavorImgSrc.slice(flavorImgSrcDash + 1, flavorImgSrc.length - 4);
    options = generateOptions();

    // The problematic line which doesn't do what I want it to: To check whether an i flavor is included in the generated random array (and if it isn't - re-generate the random array until the flavor is included) - For example: The generated random array is ['strawberry', 'peanutbutter', 'chocolate'] - if i is currently one of the 3 flavors found in the array, keep the array as it is, on the other hand if i is for example 'lemon' - re-run generateOptions() until the current i is found there - so I only get an array matching the current i
    if (!options[i].includes(flavorStr)) options = generateOptions();
}


// A function returning an array of 3 random flavors
function generateOptions() {
    var randomizedOptions = gFlavors;
    for (var i = 0; i < 3; i++) {
        shuffle(randomizedOptions);
        randomizedOptions.splice(0, randomizedOptions.length-3)
    }
    return randomizedOptions;
}

// A shuffle function for arrays
function shuffle(array) {
    array.sort(() => (Math.random() - 0.5));
}

预期结果示例: 生成的随机数组是 ['strawberry', 'peanutbutter', 'chocolate'] - 如果 i 当前是数组中找到的 3 种口味之一,则保持数组不变,另一方面,如果 i 是例如 ' lemon' - 重新运行 generateOptions() 直到在那里找到当前 i - 所以我只得到一个与当前 i 匹配的数组。

截至目前,我收到一个 TypeError: Cannot read properties of undefined (reading 'includes') at getFlavor

【问题讨论】:

  • 请添加一个问题。有什么问题和预期的结果。包括一个最小的、可重现的示例以用于运行,以便我们可以调试您的代码。在问题编辑器中查找 &lt;&gt; 图标以创建可运行的 sn-p。
  • 嗨@EmielZuurbier,已更新,谢谢
  • 您的 for 循环运行 5 个字符串,而您的 options 数组只有 3 个字符串长。所以最终,在第 4 次迭代中,将没有字符串可供选择,options[i] 将返回 undefined。这就是错误的原因。
  • @EmielZuurbier 我明白了。为了解决这个问题,我是否最好编写另一个只能在 3 上运行的循环(对于 j = 0;j
  • while 循环可能是一个很好的解决方案,用于检查您的数组是否包含数组中的任何单词,直到有一个单词为止。不过,可以肯定的是,flavorStr 的价值是多少?是'vanilla''chocolate'等吗?

标签: javascript for-loop conditional-statements


【解决方案1】:

您的 generateOptions 函数返回 void。

因此options 是未定义的,这意味着options[i] 会抛出。

【讨论】:

  • 嗨,斯卡拉,你是什么意思?我的 generateOptions 函数实际上可以工作并返回一个正确的数组,刚刚经过测试。
  • 我没有在任何地方看到退货声明。
  • 抱歉,忘记在实际的 stackoverflow 帖子中包含返回行(虽然它实际上在代码本身中)。已修复 :) 我认为上面评论中的 EmilZuurbier 已经指出了问题,但我在解决它时遇到了困难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 2020-05-22
  • 1970-01-01
  • 2017-04-10
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多