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

132
Visualizações
How to run a function twice and save output for comparison?

I have created a function that compares two arrays and makes sure they are not equal to each other. I run the function twice but I must admit it is a very poor design, however, it was the only way I could get it to work. Is there an easier way to accomplish the same thing? Maybe deepcloning with lodash? Here is my go at it.

let callCount = 0;
let values = [];
let values2 = [];
const getData = () => {
        const chart = [{ //dynamic: changes the id numbers on second call
              items: [{
                 id: 1,
                 name: 'foo'
              }, {
                 id: 2,
                 name: 'bar'
              }, {
                 id: 3,
                 name: 'bar'
              }, {
                 id: 4,
                 name: 'bar'
              }]
             }];
        if (callCount < 1) { //if and for block cannot be extracted for various reasons
            for (let i = 0; i < 5; i++) {
                const arr = chart[i].items[0];
                arr.forEach((element) => {
                    values.push(element.id);
                });
            }
            callCount++;
            return values;
        }
        if (callCount >= 1) {
            for (let i = 0; i < 5; i++) {
                const arr = chart[i].items[0];
                arr.forEach((element) => {
                    values2.push(element.id);
                });
            }
            expect(values).to.not.eq(values2);
        }
};


it ('Comparing two arrays', function () {
    getData()
    // logic that will change effectively change some of the values of `chart`
    getData()
    // is successful but wildly inefficient
}

Is there an easier way using Javascript to do the same thing without counting the number of times the function is called and running the same thing again?

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

0

Return the array each time and compare at the end of the sequence,

const getData = () => {
        const chart = [{ //dynamic: changes the id numbers on second call
              items: [{
                 id: 1,
                 name: 'foo'
              }, {
                 id: 2,
                 name: 'bar'
              }, {
                 id: 3,
                 name: 'bar'
              }, {
                 id: 4,
                 name: 'bar'
              }]
             }];
    const arr = chart[i].items[0].map(element => element.id)
    return arr;
};


it ('Comparing two arrays', function () {

    const values1 = getData()

    // logic that will change effectively change some of the values of `chart`

    const values2 = getData()

    expect(values1).to.not.eq(values2);
}

I am wondering how chart gets changed - is there some cy.get() commands involved?

If so you will have to return the result of them and nest the results

it ('Comparing two arrays', function () {
    getData().then(values1 => {

      // logic that will change effectively change some of the values of `chart`

      getData().then(const values2 => {
        expect(values1).to.not.eq(values2);
      })
    })
}
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