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

226
Visualizações
Variable value does not get updated when return-ed

I can't figure out why an integer isn't being updated. I have added event listeners to specific classes and once I hover over them the index should get updated.

That is not the case. Console log keeps on repeating that Old index: 0, New index is 1. Once I hover the next element it repeats that the Old index is 0 and the New index is 1 (value of index does not get updated).

Any help would be much appreciated. Thank you

var index = 0;
var indexArray = ["1", "2", "3"];

function addListeners() {
  let eListeners = document.querySelectorAll(".eListener");
  for (let i = 0; i < eListeners.length; i++) {
    eListeners[i].addEventListener("mouseover", function() {
      indexPlus(index)
    });
  }
}

function indexPlus(index) {
  if (index == 2) {
    console.log("Old index: " + index);
    index = 0;
    console.log("New index: " + index);
    return index;
  } else {
    console.log("Old index: " + index);
    index = index + 1;
    console.log("New index: " + index);
    return index;
  }
}

addListeners();
<span class="eListener">Some text1</span>
<span class="eListener">Some text2</span>
<span class="eListener">Some text3</span>
<span class="eListener">Some text4</span>
<span class="eListener">Some text5</span>
<span class="eListener">Some text6</span>

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

0

You are modifying the indexPlus function parameter index, not your global index variable.

To achieve that, you need to assign the return value of indexPlus to index inside your listeners. To make that more clear I've renamed the function parameter to idx.

Last things: I've shortened your check inside the ìndexPlus` function:

Instead of checking for 2 and then resetting to 0, and otherwise incrementing, you can make use of the remainder operator %:

idx = ++idx % 3;

That is a matter of how comfortable you feel with that. There's nothing wrong with your checks staying the way you implemented them.

I've also refactored your index-based loop to a for...of loop which is preferred because it improves readability.

let index = 0;
let eListeners = document.querySelectorAll(".eListener");

function addListeners() {
  for (const eListener of eListeners) {
    eListener.addEventListener("mouseover", function() {
      index = indexPlus(index)
    });
  }
}

function indexPlus(idx) {
  console.log("Old index: " + idx);
  idx = ++idx % 3;
  console.log("New index: " + idx);
  return idx;
}

addListeners();
<span class="eListener">Some text1</span>
<span class="eListener">Some text2</span>
<span class="eListener">Some text3</span>
<span class="eListener">Some text4</span>
<span class="eListener">Some text5</span>
<span class="eListener">Some text6</span>

about 4 years ago · Juan Pablo Isaza Relatório

0

In the indexPlus function use a different name for your parameter, not index as it's already globally defined

var index = 0;
var indexArray = [];

function addListeners() {
  let eListeners = document.querySelectorAll(".eListener");
  for (let i = 0; i < eListeners.length; i++) {
    //indexArray[i] = 0;
    eListeners[i].addEventListener("mouseover", function() {
      indexPlus(index)
    });
  }
}

function indexPlus(ind) {
  if (ind == 2) {
    console.log("Old index: " + index);
    index = 0;
    console.log("New index: " + index);
  } else {
    console.log("Old index: " + index);
    index = index + 1;
    console.log("New index: " + index);
  }
}

addListeners();
<span class="eListener">Some text1</span>
<span class="eListener">Some text2</span>
<span class="eListener">Some text3</span>
<span class="eListener">Some text4</span>
<span class="eListener">Some text5</span>
<span class="eListener">Some text6</span>

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