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

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

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 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!