So far, I’ve seen 3 different approaches to solving this…
-
creating a list (of some kind), and adding items to that list with each iteration…
-
using a single text (string), and appending text to it with each iteration
-
using some kind of mathematical formula (which I won’t pretend to understand!! )
It just goes to highlight the fact that there are (usually) multiple (and very different) ways to solve any particular problem.
Which one is better? Who’s to say? (that’s not the point of these exercises)… but it’s always useful to see other people’s approaches to things, and even the differences in implementation within a particular approach. It’s part of what makes working with Bubble so interesting (and what makes being part of a forum like this so valuable for learning).
My first attempt at solving this was following approach number 1… generating a list and adding to it with each iteration…
As I mentioned in a previous reply, doing this in JavaScript would be easy… just create an array of numbers, starting with 0 and 1, add the last two numbers together to generate a new number, and add that number to the list until you reach the input number.
However, you can’t do that in Bubble (as you can’t add something to a list if it’s already in it - it just won’t get added, so you end up going in circles adding 0 and 1 forever).
So my first solution here was to stick with that approach, but work around the issue of not being able to add duplicates to lists.
So instead of a list of numbers, I used a list of texts…
But that in itself doesn’t solve the problem (it doesn’t matter if you’re using numbers or texts… you still can’t add duplicate values to a list in Bubble).
So… my solutions was simply to ensure there were no duplicate values in the list.
Instead of creating a list of:
0
1
1
3
5
8
etc..
(which you can’t do in Bubble)…
…create a list of texts:
0:0
1:1
2:1
3:2
4:3
5:5
etc..
where the fist character is the index, and the last character is the number.
That way, each text in the list is unique, thus working around the known issue in Bubble of not being able to add duplicate items to a list…
so then it’s just a case of accessing the last character (using split by :
last item
) and converting that back to a number so you can do the calculation.
It’s a bit fiddly, but not too difficult, and it works perfectly well (it’s a reasonably useful and effective method of avoiding the duplicate items in a list issue) - and I’ve used this approach multiple times in real-app situations for dealing with this kind of issue.
Here’s the link to my first solution: Fibonacci Sequence 1 (bubbleapps.io)
And here is basically the same thing done with some simple JavaScript for comparison: Fibonacci Sequence js (bubbleapps.io) - notice the difference in speed - although @keith already highlighted this with his excellent side-by-side comparison of his 4 variations, and a great showcase for the usefulness of his plugins - I think sometimes it’s easy to get lost in the complexities of plugins like ListShifter and Floppy (and others)… so highlighting simple, specific use-cases for them such as these is a great way to make them more accessible and understandable as solutions to overcome fundamental Bubble limitations (I’ve certainly learnt some useful applications for them).
My second solution to this was following approach number 2 (using a single string and appending the new number to it), and I’ve honed that to down to about as elegant and clean an approach as you can get in Vanilla Bubble (albeit relying on a kind of bug to make it possible - so not really a viable long-term solution, but elegant none the less)…
I’ll post that solution here in a few days.
Approach number 3 (the mathematical answer) never really occurred to me… so great work if you came up with that one.
It would be interesting to see if anyone can come up with anything else as a viable solution to this…
Otherwise. stay tuned for the next one coming over the weekend…