Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your generateOptions function returns void.

So therefore options is undefined, meaning options[i] would throw.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda