Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
JS no devuelve el valor de una variable

Q muestra 0 y no el valor de c, ¿qué hay de malo en este código? ¿Me equivoco en la declaración de devolución?

archivo js principal

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

otro archivo js

 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 answers
Answer question

0

El número en JS es un tipo de datos primitivo y esos tipos se pasan por valor, por lo que copiará el valor de q en una nueva variable llamada c y si cambia c , q no cambiará.

puede devolver array[Math.trunc(mid)] y hacer algo como esto: document.getElementById("demo1").innerHTML = search(a,21);

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!