Q:

Refactoring decision tree

Currently I have
var l1 = [...];
var l2 = [...]; 
if (l1.length > l2.length){
    var i = l2.indexOf(l1[0]);
    if (i ! == -1){
        action_a(i);
    }else{
        action_c();
    }
} else if (l1.length < l2.length){
    var i = l1.indexOf(l2[0]);
    if (i ! == -1){
        action_b(i);
    }else{
        action_c();
    }
} else {
    action_c();
}

Which I have could refactor into the following
function foo() {
    var l1 = [...];
    var l2 = [...];
    var i = -1;
    if (l1.length > l2.length){
        i = l2.indexOf(l1[0]);
    } else if (l1.length < l2.length){
        i = l1.indexOf(l2[0]);
    }
    if (i === -1 || l1.length == l2.length) {
        action_c();
        return;
    }
    if (l1.length > l2.length){
        action_a(i);
    } else if (l1.length < l2.length){
        action_b(i);
    }
}

It's a bit better, but I'm checking for the length of the two array twice.   It's also still not as readable as I would like it to be.  
Can you review the above code and offer recommendations on how to do this better? 

A:

var l1 = [...];
var l2 = [...]; 
if (l1.length > l2.length){
    actOnDuplicateOfFirstElementIfPresent(l1, l2, action_a);
} else if (l1.length < l2.length){
    actOnDuplicateOfFirstElementIfPresent(l2, l1, action_b);
} else {
    action_c();
}

function actOnDuplicateOfFirstElementIfPresent(la, lb, action) {
    var i = lb.indexOf(la[0]);
    if (i ! == -1){
        action(i);
    }else{
        action_c();
    }
}

This Extract Method refactoring eliminates the duplication found in the first two branches.   Eliminating duplication is both an excellent motivation, and an excellent technique, for refactoring. 

A:

After re-reading the question I realized my answer was the same as your original code.  Which frankly I don't see a problem with (the upper one, the later one is worse).  
It has a clear structure which says "Okay, I'm treating three cases: less, equals and greater and doing different things in each case. 
If you want to get rid of the duplication of calls to action_c you can do like so:
var l1 = [...];
var l2 = [...]; 
if (l1.length > l2.length){
    var i = l2.indexOf(l1[0]);
    if (i ! == -1){
        action_a(i);
        return;
    }
} else if (l1.length < l2.length){
    var i = l1.indexOf(l2[0]);
    if (i ! == -1){
        action_b(i);
        return;
    }
} 
action_c();

But I would argue it's less readable as it's not immediately obvious when action_c is called as it is in the original.  But in this case it's splitting hairs IMHO. 
edit: corrected variable name

