Sum All Numbers in a Range

A solution to Free Code Camp’s “Sum All Numbers in a Range” JavaScript challenge, in a Functional Programming style.

A friend of mine hosts meetups for the Free Code Camp, which describes itself as:

We’re an open source community of people who learn to code and help nonprofits.

He organises casual meetings in coffee shops to allow participants to work on their assignments in a nice environment and help each other. If you’re looking to learn programming I recommend finding a Free Code Camp group in your area.

Today I visited such a meeting and one of the Code Campers was working on a solution for one of the JavaScript problems in the Intermediate Algorithm Scripting section. The task was to write a function that takes an array of 2 numbers as an argument and returns the sum of all the numbers between them, including the 2 numbers themselves. For example, an input of [3, 1] should result in 6, because 1 + 2 + 3 = 6. As a hint, it’s recommended you read the documentation for Math.min(), Math.max() and Array.prototype.reduce().

His solution worked, it was clearly written and well structured. It involved defining two variables for the smaller and bigger number, for example by var smaller = Math.min(arguments[0], arguments[1]), then using those values in a “for loop” which pushed an element into an array, where the value would come from the index of the current iteration in the loop. Finally he would sum this array using a callback using a simple callback that adds up the values. This uses all of the functions mentioned in the hints.

For reference here is a similar solution by someone else, sorting the input instead of using min() and max().

The main place I saw for improvement was the for loop. If we could avoid it, we could elegantly chain all our logical steps into a single statement, clearly describing our function. Using some nice syntactic sugar from ES6, I was able to distil the algorithm to the following:

  1. function header expected by Free Code Camp’s tests, x is an array
  2. create a new array to hold our range (docs), note: we return the final summed result
  3. # elements will be (max - min + 1), the +1 is needed to include the upper value, e.g. 5..10 = (10 - 5 + 1) = (5 + 1) = 6
  4. lambda populates array with incrementing values starting from min (the array keys k already increment)
  5. reduce array by summing current number c into sum s
  6. Note: min() and max() expect several numbers as arguments, not a single array, ...x is how we spread x into arguments

I like this solution because I feel it better describes what we’re trying to make this function do and it is very concise without being unreadable. Several of the features it uses are only available in ES6 and it may be even better with ES7’s comprehensions, in fact I spent quite a bit of time searching for these features in JavaScript because they’re available in other languages like Python and Ruby.

Hopefully someone trying to write more concise and functional JavaScript code finds this post useful, or perhaps a Code Camper will stumble upon it and get inspired in their learning. :-)

International Christian Fellowship of Budapest

ICFB LogoI joined the International Christian Fellowship of Budapest (ICFB) not knowing what to expect. Admittedly, having never been part of any kind of “Christian Group” before, initially I had my doubts. I was more of a quiet Christian, striving to show kindness but rarely talking to another person about Christianity. So it was with hesitation that I told Berni I’d join her to visit a group for Christians, which her friend Eszter had invited her to.

On the way home that Friday, chatting about plans for the weekend, my friend Attis told me the reason he’s got his guitar with him is because he’s on the way to an international Christian meet-up for students and young professionals, and asked me if I’d like to come along. This sounded cool and I like spontaneity but I’d already set plans for the evening with Berni. Of course it didn’t take long before we figured out we’d both been invited to the same event; I laughed all the way there.

I’ve been participating in ICFB’s Friday meet ups for almost three years now. I love the group and I’ve made some good friends there. My favourite part is singing together, so I always bring a guitar with me. I’d say even among other IFES groups in Hungary we’ve made a name for being lively and singing with spirit.

Árpád's map

Árpád’s map

The most important part of ICFB, after the C, is I: International isn’t just in the name. Our group is made up of people from countries all over the world. Árpád has a map of the world in his garden with names painted on to show where people he’s met are from. In a similar way, of all the songs we sing, my favourite is when we sing “How Great Is Our God” and everyone takes a turn singing the chorus in their own language.

However, we don’t just sing and we don’t just meet on Fridays. People often come sharing verses, insights or personal questions that make for good discussion. In good weather we might meet in parks on weekends and sometimes I bring my rugby ball to share a bit of my culture through touch-rugby.

That’s just me though; everyone has their own personal part in ICFB that makes it special for them and even though we’re all so different we all have one thing in common.

This was originally written as a guest post for mekdsz, the Hungarian evangelical Christian student fellowship. You can read their Hungarian translation here:
A nemzetközi helyzet tovább fokozódik.

Lean Poker

I had a lot of fun attending a Lean Poker event last weekend!

Me at Lean Poker

Me at Lean Poker

It’s a type of event where programmers get together, form teams and spend the day writing code competitively, to see who can write the best automated online-poker player. We don’t play for money but for pride, and the main aim is to practise writing beautiful code and lean principles. That said, given the time constraint of a single day, the focus is usually on Deliver as fast as possible and by the end I’m flurrying around to keep errors out of the code. We try to get quick feedback during the day (more on that later) but I thought I’d do a write about the event to give people who haven’t attended one of these before an idea of what it’s like!

Continue reading

All Hallows Day

Candles lighting up the cemetery on Halloween in Vaszar.

Candles lighting up the cemetery on Halloween in Vaszar.

We didn’t dress up and go trick-or-treating for Halloween in Namibia, but Jack-o-Lanterns and spooky costume parties are what come to mind when I think of the time around the transition from October to November. In Hungary –and it turns out many countries in this area– it’s celebrated a bit differently and spooky costume Halloween parties have only started to become popular in the 21st century.

On 1 November, Hungarians travel to the countryside for All Saints’ Day and All Souls’ Day on 2 November. They visit the cemeteries where their ancestors are buried and decorate the graves with flowers and candles. It looks really beautiful at night and I took some photos when we were in Vaszar on the weekend. It was a dark night and the pictures are mostly out of focus, but I think they’re still pretty.

It turns out Halloween is a combination of the words Hallow (meaning holy or saint) and e’en (a contraction of even, which is the Scots spelling of eve or evening), and the celebration has a long Christian and Celtic folk history.

Just Married!

On 19 July 2014 I married Berni in the St György Roman Catholic Church in her home town, Vaszar. Thank you to everyone for celebrating with us as and for your kind wishes on our special day, as we bound our lives together with love!

With love from Berni & Siôn — 19 July 2014

With love from Berni & Siôn — 19 July 2014

There are lots more photos coming soon on our wedding website!

Cat vs Crow

Very strange. On Thursday, on my way to work I saw a cat in the park being harassed by a crow so I recorded it. Unfortunately the video isn’t very sharp because the animals were far away and I was recording them with my phone. I thought it was really funny, as if the crow had a big problem with the cat being there, meanwhile the cat completely ignores the bird.

YouTube View directly

Funniest of all, I walked the same route the next day and they were there again doing the exact same thing!

Website transfer complete!

Namibia to HungaryThe site is back up on line! For more than a month it’s been down while I transferred it from the Namibian server to a new Hungarian web host and now it’s back. Apologies to anyone who wanted to access the site during this time and thank you for your patience. Now that things are working more or less smoothly I should be able to post some fresh content! :)

Switch Colours in the GIMP

GIMP Colour SwitcherAfter my previous discovery I thought surely there should also be an easy keyboard shortcut for swapping the foreground and background colours in the GIMP, even though it doesn’t seem to be documented anywhere. There is. Just press X.

Actually I think it would be useful if this was shown in the tooltip text when you hover over the colour switching button, like it does with the other tools. If you’re curious, here is a list of 74 keyboard shortcuts for the GIMP (including this one) that you might not have known otherwise. I think keyboard shortcuts are great for editing pictures because you can change tools (and now also swap colours) without moving your cursor away from where you’re drawing!