Counting up and down in recursive manner in JS

I am currenty working on functional programming techniques. There are topics about this issue [ especially there is one about java ] but not about 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. 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.

    Reply
  3. 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!

    Reply
  4. 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!

    Reply
  5. 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!

    Reply
  6. 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

    Reply
  7. 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
    if your web host is OK? Not that I’m complaining, but slow loading instances times will very frequently affect your placement
    in google and could damage your high-quality score if ads and marketing with Adwords.
    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

    Reply
  8. 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.

    Reply
  9. 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!!

    But yeah, thanks for spending the time to talk about this topic
    here on your web site.

    Reply
  10. 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.

    Reply
  11. 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.

    Reply
  12. 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!

    Reply
  13. 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.

    Reply
  14. 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
    for getting off-topic but I had to ask!

    my web page 2022

    Reply
  15. 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.

    Reply

Leave a Comment