Tuesday, 21 March 2017

CodeWars

JavaScript developers from Europe

Made good progress with this quite quickly (once I identitfied that my undefined output was caused by me not calling the function - D’Oh!).

Basic Approach

Once I had the key callback logic down (list.[method](function(object) { ... })) I literally swapped in various methods to figure out the best approach: forEach, map, find etc.

I realised that since filter would only return objects for which the function statement was true, I didn’t need to do anything else except count the members of the resulting array with .length:

    function countDevelopers(list) {
        var responses = list.filter(function(object) {
            return object.continent === 'Europe';
            }
        ); 
        return responses.length; 
    }

Refactoring

I want to start using ES6 notation a bit more since it’s more concise and professional looking ;-)

Got the above code down to:

    const countDevelopers = list =>
      list.filter (obj => obj.continent === 'Europe').length;

Pretty pleased with myself!

Oh No!

Of course I hadn’t read the instructions properly. Wondering why the tests aren’t passing, I eventually realise that not all these devs from Europe are JavaScript devs! Doingggg!

Can I save this without a huge restructuring exercise?!

YES!

With the addition of && obj.language === 'JavaScript'.

Phew!