Trying to get to grips with a functional style in javascript