Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

163
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda