mapping & loops - counting up or counting down I am building a time tracking application and my requirement says that any hours past 40 total should be counted as overtime and not regular hours.

Which function is more clear? What can be done to clarify the function? Even the second one at ~ 10 lines seems wordy

Option #1

function calculateOvertime(days) {
    var totalHours = 0;
    var over40 = false;
    return $.map(days, function(hours) {
        var ot = 0;
        totalHours += hours;
        if (totalHours &gt; 40 &amp;&amp; !over40) {
            ot = totalHours - 40;
            hours = hours - ot;
            over40 = true;
        } else if (over40) {
            ot = hours;
            hours = 0; 
        }
        return { reg: hours, ot: ot};
    })    
}
calculateOvertime([16,16,16,16]);


Option #2

function calculateOvertime(days) {
    var hoursUntilOT = 40;
    return $.map(days, function(hours) {
        var ot = 0;
        hoursUntilOT -= hours;
        if (hoursUntilOT &lt; 0) {
            ot = hoursUntilOT * -1;
            hours = hours - ot;
            hoursUntilOT = 0;
        }
        return { reg: hours, ot: ot};
    })    
}
calculateOvertime([16,16,16,16]);
You can accomplish your task using only core Javascript, with no reference to JQuery code, in about that many lines. JQuery is overkill for this task. 

I applied the following changes to make your code more readable:


Reduced knowledge required to understand it (removed JQuery).
Reduced the complex portions (see iCurrentOvertime).
Added Hungarian notation; i.e. prepending integers with "i", arrays with "a", etc.
Encased added values together within parentheses (e.g. iCurrentHours+iTotalHours becomes (iCurrentHours+iTotalHours)).


The new code is below:

function calculateOvertime(days) {
  var aHours = [];
  var iTotalHours = 0;
  for(var i=0;i&lt;days.length;i++){
    var iCurrentHours = days[i];
    var iNewHours = (iCurrentHours+iTotalHours);
    var iCurrentOvertime = Math.max(0,(iNewHours - Math.max(iTotalHours,40)));
    iTotalHours = iNewHours;
    aHours.push({
      reg: (iCurrentHours - iCurrentOvertime),
      ot: iCurrentOvertime
    });
  }
  return aHours;
}
calculateOvertime([16,16,16,16]);