I Feel Like A Developer
I Feel Like A Developer

I Feel Like A Developer

I’ve had a very interesting week in various respects, and my work as a developer has certainly been part of that.

A week on Friday ago, our technical director asked me to create some functionality to display to the customer the cheapest available delivery slot within the next 7 days.

My initial thought was “how the fuck am I going to do that”.  I felt a little overwhelmed with the idea at first – I’ve been there not too far off 6 months now, and I am still a junior.  I am aware of my strong points (albeit at relative scale) and also have become very aware of my weaknesses.

This involved calls to the API which I had just about got my head around.

When it comes to writing functions, I do pretty much all my draft coding in the console, so the first thing was to write a GET request to ensure that I could retrieve the timeslots data that I required – a customer can, for example, pay more for delivery first thing in the morning, or pay less for delivery during our less busy days which are normally Tuesday/Wednesday.

I realised at this point that I was becoming very comfortable in making requests to the API – although I had some limited knowledge of working with APIs, I now feel like I actually understand them, and can even run functions off their successful calls.

However, being able to retrieve the data for the current day only was not the task.  I had to retrieve the data for 7 days, and with the added complication that I had to take into account the first possible date of delivery – for example if you order after 11am on a Monday, the first delivery possible day is a Wednesday.  Weekends and bank holidays add extra complications – though there already was a function for this.

So I once I had worked out the first date and the last date, I then had to get the date into a form that you iterate and add 1 to – ie I am running the request for the first day, then the request repeats but adds 1 day – and so on until the maximum.

This was my first infinite loop at Lovespace, where I had set the calculation to keep on going until infinity.  Hmmm.  Chrome was not happy.

It took quite a few attempts to get the loop right.

// Create first and last date (6 days later) to iterate through

            var firstDate = new Date();
            firstDate.setDate(firstDate.getDate() + collectionOffset);
            var endDate = date.setDate(date.getDate() + 6 + collectionOffset);
            // Create empty array
            var object = [];
            // Get available timeslots for each day and insert into empty array
            for (var d = firstDate; d <= endDate; d.setDate(d.getDate() + 1)) {

My task was complicated further because the API didn’t recognise that format of date, so as part of the loop I had to then reformat it into a form that our API would recognise.

A little frustrating to get this right, and it took me a while to work out a good, clean, simple solution for it – but it worked.

I had data with which to play with.  Except it was in the form of an array of arrays of objects (don’t ask if you aren’t a coder).

So I removed one level of array with:

var merged = [].concat.apply([], object);

Then I had to work out how to find the cheapest available slot.  What I had was an array of objects – each object had the amount of available timeslots for that day, with a price, a date and other information.

There were probably a few ways to do it, but the most intuitive to me, was to sort by price and then by date, to display the next available of the cheapest options.

I knew how to sort by one criteria – but not two.  This took a bit of playing with, and some help from Stack Overflow (what would we do with it?):

            merged.sort(function (a, b) {
                if (a.Price === b.Price) {
                    return new Date(a.Date).getTime() – new Date(b.Date).getTime();
                } else if (a.Price > b.Price) {
                    return 1;
                } else if (a.Price < b.Price) {
                    return -1;
                }
            });

Finally I had a list of options sorted by price and date.  I then just had to take the first item, turn the data into something human readable, then get it to display.

Getting it to display at the appropriate moment was then also a challenge – where in the 10,000 lines of code that runs our website would I need to place it to ensure that it always ran?

It wasn’t just as simple as running it after it displayed – as the customer has the option to change delivery address, which may have different pricing and times, so would then need to run again.

The function in the end came to 70 lines, including comments.  Something I simply wouldn’t have been able to do a year ago, and probably not prior to joining Lovespace.  I am sure that an experienced developer would be able to improve it, and maybe in a year or so I will be able to also.

It showed to me that I am making really good progress in becoming a web developer.  I do still have to pinch myself sometimes when I think about how I have changed from doing a shit credit control job for a miserable company, to doing what I have been working towards for the last few years, for a really good company.

I am definitely still a junior developer but every day I come closer to losing that ‘junior’ tag in my mind.

ps Just realised this site has some problems to fix since I last updated WordPress…joy!

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *