# Counting up and down in recursive manner in JS

I want to create a recursive function that can first, count up until the limit I decide, from the number I indicate and then start counting down when the limit is reached. I can already do this with a for loop but it feels like hard-coded since i am supplying the numbers in the loop.

Basically something like this:

``````counter(0,10);
// 0, 1, 2, 3 ... 10, 9, 8... 0
``````

Here’s what I thought of:

``````counter = (number, limit) => {
limit !=== 0

if ( number = limit ) {
counter(number -1, limit -1)
console.log(number)
} else if ( number < limit ) {
counter(number + 1, limit + 1)
}
}
``````

Idea behind this is, if the number is lower than the limit count up, if they are equal, decrement each argument by 1 to continue satisfying the first if condition.

When I run this command on v8, it gives a rangeError "maximum stack size reached".

Also, this should not be an infinite loop.

For loop version:

``````for (let i = 0; i < 11; i++ ) { console.log(i) }
for (let i = 9; i < 11 && i > -1; i--) { console.log(i) }
``````

### 143 thoughts on “Counting up and down in recursive manner in JS”

1. Pingback: keto bars
2. viagra 25 mg – generic viagra online us pharmacy

3. Pretty component to content. I simply stumbled upon your website and
in accession capital to say that I acquire actually loved account your weblog posts.

Any way I’ll be subscribing to your augment or even I fulfillment you
access persistently quickly.

4. Thanks for ones marvelous posting! I really enjoyed reading it, you happen to be a great author.
I will make certain to bookmark your blog and may come back in the future.
I want to encourage that you continue your great job, have
a nice evening!

5. Someone necessarily help to make significantly articles I’d state.
This is the first time I frequented your website page and to this point?
I surprised with the research you made to create this
actual publish extraordinary. Wonderful activity!

6. Hi there, I enjoy reading through your article post. I like to
write a little comment to support you.

7. I constantly spent my half an hour to read this website’s articles daily along
with a mug of coffee.

8. can i buy prednisone over the counter in mexico – buy prednisone how much is prednisone 20 mg

9. Its not my first time to go to see this web page, i am browsing this web site dailly and take good facts from here everyday.

10. Hello, I enjoy reading through your post. I like to write a little comment to support you.

11. First of all I want to say great blog! I had
a quick question which I’d like to ask if you do not mind.
I was interested to know how you center yourself and clear your
thoughts prior to writing. I’ve had a difficult time clearing my mind in getting my ideas out.

I truly do enjoy writing however it just seems like the first 10 to 15 minutes are lost just trying
to figure out how to begin. Any suggestions or hints? Thank you!

12. Fantastic beat ! I would like to apprentice while you amend your website, how could i subscribe for a blog web
site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear concept

13. hello there and thank you for your info â€“ I
have definitely picked up something new from right here.

I did however expertise some technical issues using this site, as I experienced to reload the web site many times
previous to I could get it to load correctly. I had been wondering
Anyway I’m adding this RSS to my email and could look out for a lot more of your respective exciting content.

Ensure that you update this again very soon. scoliosis surgery https://0401mm.tumblr.com/ scoliosis
surgery

14. I used to be recommended this web site by means
of my cousin. I am not certain whether this submit is written by means
of him as nobody else realize such special about my problem.
You’re wonderful! Thank you! ps4 https://tinyurl.com/45xtc52b ps4 games

15. cost of prednisone 10mg tablets – apo-prednisone prednisone 20mg tablets where to buy

16. ivermectin tablets order – site ivermectin pills canada

17. Thanks for sharing such a fastidious thinking, piece of writing is
fastidious, thats why i have read it entirely

18. May I just say what a comfort to uncover somebody that truly knows
what they are talking about online. You definitely realize how to
bring a problem to light and make it important.
More and more people really need to check this out and understand this side of your
story. I was surprised you are not more popular
given that you definitely possess the gift.

19. An outstanding share! I have just forwarded
this onto a coworker who had been conducting a little research on this.
And he in fact bought me breakfast because I found it for him…
lol. So allow me to reword this…. Thanks for the meal!!

20. This is my first time go to see at here and i am truly pleassant to

21. Does your blog have a contact page? I’m having a tough time
locating it but, I’d like to shoot you an e-mail.
I’ve got some ideas for your blog you might be
interested in hearing. Either way, great website
and I look forward to seeing it expand over time.

22. Hi, Neat post. There’s a problem together with your site in web explorer,
may check this? IE still is the marketplace chief and a
big component of other people will miss your wonderful writing due to this problem.

23. Wow, superb blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your website is excellent, let alone the content!

24. Good day! I know this is kinda off topic nevertheless I’d
figured I’d ask. Would you be interested in trading links or maybe guest authoring a blog post or vice-versa?
My website covers a lot of the same subjects as yours and I believe we could greatly benefit from each other.
If you’re interested feel free to shoot me an email.
I look forward to hearing from you! Great blog by the way!

25. I’m amazed, I must say. Rarely do I encounter a blog that’s both educative and engaging, and let me
tell you, you’ve hit the nail on the head. The problem is an issue that too few men and women are speaking intelligently about.

I’m very happy I came across this during my hunt for something relating to this.

however, you seem like you know what you’re talking about!
Thanks

27. Hello would you mind sharing which blog platform
you’re using? I’m looking to start my own blog in the near future
but I’m having a difficult time choosing between BlogEngine/Wordpress/B2evolution and
Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for
something completely unique. P.S Sorry

my web page 2022

28. I was recommended this blog by my cousin. I am not sure whether
this post is written by him as no one else know such detailed about my problem.

You’re wonderful! Thanks!

29. Hi mates, its wonderful post regarding teachingand fully defined, keep it up all
the time.

30. Attractive part of content. I simply stumbled upon your website and in accession capital
to claim that I acquire in fact loved account your weblog
posts. Any way I will be subscribing on your
feeds or even I fulfillment you access consistently quickly.