Loops/Continue: Difference between revisions

Content added Content deleted
Line 804:
output += ", ";
}</lang>
 
 
Stepping back from any assumption that repetitive patterns of computation necessarily entail iteration, and using a functional idiom of JavaScript, we can make the value of one or more subexpressions in a ''reduce()'' fold conditional on any special cases that we define.
 
For example:
 
<lang JavaScript>function rng(n) {
return n ? rng(n - 1).concat(n) : [];
}
 
console.log(
rng(10).reduce(
function (a, x) {
return a + x.toString() + (x % 5 ? ', ' : '\n');
}, ''
)
);</lang>
 
Output:
<lang JavaScript>1, 2, 3, 4, 5
6, 7, 8, 9, 10
</lang>
 
=={{header|jq}}==