How can I do recursion?

Options

I have the following table setup

I want to be able to enter a value of 7 and I want to get an array like [7,5,3,2,1].

Or if I enter a value of 3 I want to get an array like [3,2,1].

Generally speaking, when you do recursion you pass the function as a parameter to itself e.g.

int fib(int n)  {

    if (n <= 1) {

      return n;

}

   return fib(n - 1) + fib(n - 2);

}

This is what I currently have

https://www.loom.com/share/7fd496610665440f9354fad604a008d1

How can I pass a function to itself in Xano and/or what exactly am I don't incorrectly?

Best Answers

Answers

  • nocodetalks
    nocodetalks Member ✭✭
    Options

    Instead of recursion-


    Step 1- Query all the record with custom query where id<="value" and sort by "id" - descending.

    And return the "table.id" as the json array.

  • pete letkeman
    Options

    Okay, let's try a different example or two

    Input 9, output [3,2,1]

    Input 8, output [5,3,2,1]

    There is no guarantee that the child's values will be less than the parent's values or that there will be an easy-to-guess sequence for the child's values as the system is used.

    The only guarantee is that at some point in time there will not be any records found for the given parent id.

  • nocodetalks
    nocodetalks Member ✭✭
    Options

    @pete letkeman How are you getting [3,2,1] or [5,3,2,1]?

  • pete letkeman
    pete letkeman Member
    edited November 2022
    Options

    Opps, I did that wrong.

    It should be the following:

    Input 8, would return [3,2,1]

    Id = 8, parentId = 3 (root)

    Id = 3, parentId = 2 (super admin)

    id = 2, parentId = 1 (everyone)

    id = 1, parentId = null

    --------------------------

    Input = 9, would return [2,1]

    Id = 9, parentId = 2 (super admin)

    id = 2, parentId = 1 (everyone)

    id = 1, parentId = null

  • pete letkeman
    Options

    That looks like it would work.

    I'm not too sure that I'd call that recursion as the function/method is not calling itself, which is fine as not everything needs recursion.

    That said, can you do recursion programming with Xano?