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

230
Views
why it reverse the opration after it do?

I try to add a numbers from 1 to 10 by JavaScript using the concept of recursion and it didn't work as intended. it revers the operation and it come up with the first result and when i use variable to store the result i get it come up with undefined. here is the code

var total = 0, count = 1;
function sum(total, count){
    if(count <= 3){
        total +=count;
        count++;
        sum(total, count);
    }else{
        return total;
    };
}
var result = sum(total, count);
console.log(result);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not returning from your first if condition. Default return value for any function is undefined.

var total = 0, count = 1;
function sum(total, count){
    if(count <= 3){
        total +=count;
        count++;
        return sum(total, count);
    }else{
        return total;
    };
}
var result = sum(total, count);
console.log(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!