Loops/While: Difference between revisions
Content added Content deleted
Line 740: | Line 740: | ||
}</lang> |
}</lang> |
||
In a functional idiom of JavaScript, however, we can not use a |
In a functional idiom of JavaScript, however, we can not use a While '''statement''' to achieve this task, as statements return no value, mutate state, and can not be composed within other functional expressions. |
||
Instead, we can define a composable loopWhile() function which has no side effects, and takes 3 arguments: |
Instead, we can define a composable loopWhile() '''function''' which has no side effects, and takes 3 arguments: |
||
:#An initial value |
:#An initial value |
||
:#A function which returns some derived value |
:#A function which returns some derived value, corresponding to the body of the While loop |
||
:#A conditional function |
:#A conditional function, corresponding to the While test |
||
<lang JavaScript>function loopWhile(varValue, fnDelta, fnTest) { |
<lang JavaScript>function loopWhile(varValue, fnDelta, fnTest) { |