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

278
Views
why this javscript code giving undefined as a result? (Calculating GCD), I don't know why it's not entering if block

I don't know why its not entering if block.Maybe because of type coercion. Please correct me and tell me what's the mistake in this code.

function calculateGCD(a, b) {
  if (b === 0) {
    return a;
  } else
    console.log(a, b);
  a > b ? calculateGCD(b, (a % b)) : calculateGCD(a, (b % a));
}

function main() {
  let n1, n2, gcd;
  n1 = +prompt("enter 1st number?");
  n2 = +prompt("enter second number?");

  gcd = calculateGCD(n1, n2);

  document.write(gcd);
}


main();

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In the function you should return the result.

    function calculateGCD(a, b) {
      let result
      if (b === 0) {
        return a;
      } else {
       console.log(a, b);
       a > b ? result = (b, (a % b)) : result = (a, (b % a));
    }
   return result
  }
about 4 years ago · Juan Pablo Isaza 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!