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

284
Views
Use Recursion to Create a Countdown | FreeCodeCamp Recursion problem

var a=[];
// Only change code below this line
function countdown(n){
  
  if(n>=1){
    
    countdown(n-1);
    console.log(n);
    a.push(n);
    return a;
  }
  else{
    return [];
  }
}
console.log(countdown(5));

Here I want to know after the recalling of the countdown function why the n is printed like 1,2,3,4,5? It should be 5,4,3,2,1?

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

0

In the initial call to countdown (where n=5) you recursively call countdown and then print n.

about 4 years ago · Juan Pablo Isaza Report

0

Switch the order of console.log(n); and countdown(n-1); and you will have expected behavior.

var a=[];
// Only change code below this line
function countdown(n){
  
  if(n>=1){
    
    console.log(n);
    countdown(n-1);
    a.unshift(n);
    return a;
  }
  else{
    return [];
  }
}
console.log(countdown(5));

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!