If condition in a for loop to ensure a string is found in an array


Using an array of 3 words which is randomly generated from another function, I’m trying to get a function to check whether a certain i word in a loop, which is given from the src of an image file, is found in the array generated from that other function, and in case it isn’t – that other function would run again until the word would be included in the array.

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

Example of expected result:
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.

As of now, I am receiving a TypeError: Cannot read properties of undefined (reading ‘includes’) at getFlavor

Source: JavaSript – Stack Overflow

November 19, 2021
Category : News
Tags: conditional-statements | for-loop | javascript

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.