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

152
Visualizações
JS not returning value of a variable

Q display 0 and not the value of c, what is wrong with these code? Am i wrong in the return statement?

main js file

var q=0;
search(a,21,q);
document.getElementById("demo1").innerHTML = q;

another js file

function search(array, target, c) {
  left = 0;
  right = array.length - 1;

  while (left <= right) {
    mid = (left + right) / 2;
    if (array[Math.trunc(mid)] == target) {
      c = Math.trunc(mid);
      console.log(c);

      return array, target, c;

    } else if (target < array[mid]) {
      right = Math.trunc(mid) - 1;
    } else {
      left = Math.trunc(mid) + 1;
    }
  }
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Number in JS is a primitive data type and those types are passed by value, so it will copy the value of q into a new variable named c and if you change c, q will not change.

you can return array[Math.trunc(mid)] and do something like this: document.getElementById("demo1").innerHTML = search(a,21);

about 4 years ago · Santiago Trujillo 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