Avanesyan,
function limit(max) {
return function(query) {
if (max <= 0) { query = 0;}
else {
if (max - query >= 0) {
max -= query;
} else {
query = max;
max = 0;
}
}
return query;
};
}
;
var arr = [4, 4, 9, 7];
var out = arr.map(limit(15));// [4, 4, 7, 0]
alert(JSON.stringify(out));