Loops/Continue: Difference between revisions

Content deleted Content added
PatGarrett (talk | contribs)
Hout (talk | contribs)
Line 804: Line 804:
output += ", ";
output += ", ";
}</lang>
}</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}}==
=={{header|jq}}==