Solution to a recursive problem (code kata)

Source code

// str is the string that's been built so far.
// limit is the total number of bracket pairs
//
// e.g. initial call: brackets('', 3)
function brackets(str, limit)
{
    // rules
    //
    // if brackets in str balance (includes empty str), must
    //   have open bracket
    // if all open bracket used, must have close bracket 
    // otherwise, can have either bracket

    if (str.length == 2 * limit)
    {
        return str;
    }

    var open = close = 0;

    for (i = 0; i < str.length; i++)
    {
        if (str[i] == '(')
        {
            open++;
        }
        else if (str[i] == ')')
        {
            close++;
        }
    }

    if (open == close)
    {
        return brackets(str + '(', limit);
    }
    else if (open == limit)
    {
        return brackets(str + ')', limit);
    }
    else
    {
        return new Array(brackets(str + '(', limit),
                         brackets(str + ')', limit));
    }

Tweet

Comments

Fri 12 Dec 2014 11:27

much does cialis cost

much does cialis cost said:

I am regular visitor, how are you everybody?

This post posted at this website is really

fastidious.

Sat 13 Dec 2014 18:02

canada cialis generic

canada cialis generic said:

Hi there, You have done a fantastic job. I’ll certainly digg it and personally

suggest to my friends. I’m sure they will be benefited from this

web site.

Tue 16 Dec 2014 13:57

buying cialis online canada

buying cialis online canada said:

My brother suggested I may like this website. He was totally right.

This submit truly made my day. You can not believe just how so much time I had spent for this info!

Thanks!

Tue 16 Dec 2014 19:09

cialis generic pharmacy

cialis generic pharmacy said:

When someone writes an piece of writing he/she maintains the plan of a

user in his/her brain that how a user can understand it. Therefore that’s why this post is amazing.

Thanks!

Thu 18 Dec 2014 09:35

canadian cialis

canadian cialis said:

You really make it appear really easy along with your presentation however I find this matter to be really something that I believe I’d never understand.

It seems too complex and very extensive for me. I’m taking a look ahead

to your next publish, I will attempt to get the cling of it!

Tue 23 Dec 2014 08:34

generic cialis from canada

generic cialis from canada said:

Definitely believe that which you said. Your favorite reason seemed to be on the web the simplest thing to be aware of.

I say to you, I certainly get irked while people consider

worries that they plainly don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects ,

people can take a signal. Will probably be back to get more.

Thanks

Wed 24 Dec 2014 08:54

cialis dosage side effects

cialis dosage side effects said:

I think this is among the most vital information for me.

And i’m glad reading your article. But wanna remark on some general things, The web site style is perfect, the articles

is really great : D. Good job, cheers

Tue 30 Dec 2014 01:39

buy cialis canada

buy cialis canada said:

Very nice post. I just stumbled upon your blog and wished to say that I have really enjoyed surfing around your blog posts.

In any case I will be subscribing to your feed and I hope you write again soon!

Tue 30 Dec 2014 07:48

canadian pharmacy viagra

canadian pharmacy viagra said:

Thanks in support of sharing such a pleasant thinking, piece of

writing is pleasant, thats why i have read

it completely

Tue 30 Dec 2014 12:41

viagra for sale to canada

viagra for sale to canada said:

Pretty! This was a really wonderful article. Many thanks for supplying these details.

Thu 1 Jan 2015 02:12

drugstore online

drugstore online said:

Appreciation to my father who stated to me on the topic of this blog, this blog is really amazing.

Thu 8 Jan 2015 13:58

cialis in canada

cialis in canada said:

Heya i’m for the primary time here. I came across this board and I to find

It really useful & it helped me out a lot. I’m hoping to provide something back

and aid others like you helped me.

Sat 10 Jan 2015 16:25

cialis from canada

cialis from canada said:

Thanks for some other informative blog. Where else may I get that kind of information written in such a perfect approach?

I've a venture that I’m simply now working on, and I've been on the look

out for such information.

Mon 12 Jan 2015 07:01

canadian pharmacy

canadian pharmacy said:

Hi there! I know this is kind of off topic but I was wondering if you knew where I could

find a captcha plugin for my comment form? I’m using the

same blog platform as yours and I’m having problems finding one?

Thanks a lot!

Sat 17 Jan 2015 09:38

Viagra cialis levitra canada

Viagra cialis levitra canada said:

May I simply just say what a relief to uncover an individual who

actually understands what they’re discussing on the web.

You definitely realize how to bring a problem to light and make it important.

A lot more people need to read this and understand this side

of your story. I can’t believe you aren’t more popular since you certainly have the gift.

Tue 20 Jan 2015 23:17

buy cialis from canada online

buy cialis from canada online said:

Hello, just wanted to say, I enjoyed this post. It was practical.

Keep on posting!

Wed 21 Jan 2015 09:34

cialis canada

cialis canada said:

Good day! Do you know if they make any plugins to protect against hackers?

I’m kinda paranoid about losing everything I've worked hard on. Any

recommendations?

Fri 23 Jan 2015 11:16

north west pharmacy canada

north west pharmacy canada said:

The Generic pharmacy is basically reasonably priced regardless if you

are from whichever parts of the culture.

The cost is affordable for anyone and everyone; you will need to drive of buying it.

When decision is manufactured, look for the suitable store.

After purchasing the medication, you will have utilized it.

Just consume the products with plain mineral water.

drugs medications

Leave a comment