Showing posts with label codepod. Show all posts
Showing posts with label codepod. Show all posts

Tuesday, 21 March 2017

CodeWars - Is Ruby Coming?

Is Ruby Coming?

Not a lot to say about this one.

function isRubyComing(list) {  
  return list.filter(obj => obj.language === 'Ruby').length > 0;
}

I was trying to find a less hokey way of asserting the return boolean, but all my efforts at double-negating the function statement failed because an empty array is still a truthy value.

Only on completion did I see what I was looking for in other people’s solutions array.some()

function isRubyComing(list) {
  return list.some(obj => obj.language === 'Ruby');
}

Nice and declarative. I love it.

CodeWars - Greet Developers

Greet Developers

Another good one easy one from Piotr.

After the last exercise I went straight for the .forEach method. I was pretty sure I could just obj.greetdevelopers = [...] and so it was.

Initially I tried to use this.firstName and this.language but that didn’t work and it seems obvious to me now because at the time this is invoked it refers to the object in which the function greetDevelopers’s execution context is created. In this case the global object, I think.

So I replaced it with obj.

Here’s the code I ended up with:

function greetDevelopers(list) {
    list.forEach ((obj) => obj.greeting = "Hi " + obj.firstName + ", what do you like the most about " + obj.language +"?");    
    return list;
}

The function acts on each object in turn, changing it. The list object (and array) is not changed per se - it is identical at the start and end - but each object element within it has been amended.

I couldn’t get the => to work properly. Need to find out about that.

Done.

Other Solutions

Looking at the existing solutions on CodeWars, I notice the top differ slightly from mine:

    function greetDevelopers(list) {
      return list.map(x => Object.assign({}, x, {
        greeting: `Hi ${x.firstName}, what do you like the most about ${x.language}?`
      }))
    }

Object.assign({}, x, {...})) and the ${ } escapes (or whatever you want to call them)…

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!